Written a few years ago ,Ocr The screenshot identifies that the paid software can be used for its own use when it comes out frequently
Implementation scheme
1、 Call any screenshot software screenshot , Auto save to clipboard
2、python Call up the clipboard , Get a temporary local picture saved as a fixed location in the picture stream
3、 Identify local images , Copy the text content back to the clipboard
import ioimport osimport base64import PILimport jsonpathimport pyperclipfrom aip import AipOcrfrom PIL import Image, ImageGrabroot_path = os.path.abspath(".")_app_id = 'xxx' # From Baidu , Ali , Tencent, etc. Will do , apply orc Service account , There is a free identification amount every day , Enough for personal use _api_key = 'xxx'_secrect_key = 'xxx'# Baidu cloud api object client = AipOcr(_app_id, _api_key, _secrect_key)# ocr The maximum image size recognized cannot exceed 4 mega img_max_size = 4096def get_file_content(file_path: str): """ Read the file """ with open(file_path, 'rb') as fp: return fp.read()def get_size_kb(file_path: str): """ Get file size (kb) """ size = os.stat(file_path).st_size return round(size / 1024)def ocr_img(img: bytes, wrap: bool = True): """ Identify the text content in the picture :param img: picture base64 :param wrap: Do you want to change lines , Default newline :return: """ data = client.basicGeneral(img) t = jsonpath.jsonpath(data, '$.words_result[*].words') if t and wrap: return '\n'.join(t) return ' '.join(t)def get_image_from_clipboard(): """ Save clipboard image to local """ img = ImageGrab.grabclipboard() if isinstance(img, Image.Image): img.save('test.png', 'png') print(" Save clipboard picture ") else: print(" No picture on clipboard ")def get_image_base64_from_clipboard(): """ Save the clipboard image as IO flow """ img_buffer = io.BytesIO() img = ImageGrab.grabclipboard() if isinstance(img, Image.Image): img.save(img_buffer, 'png') # b64 = base64.b64encode(img_buffer.getvalue()) b64 = base64.encodebytes(img_buffer.getvalue()) print(b64) print(" Save the clipboard image as Base64 code ") else: print(" There is no picture in the clipboard ")def set_clipboard_text(msg: str): """ Write clipboard contents """ pyperclip.copy(msg)def get_clipboard_text(): """ Read the contents of the cutting board """ return pyperclip.paste()
# -*- coding: utf-8 -*-from ApiOcr import *def ocr_clipboard(): """ It is used to obtain the picture information of the clipboard , And save to local , Then proceed """ # Get the picture in the clipboard img = ImageGrab.grabclipboard() if img is None: print(' There is no picture in the clipboard ') return if isinstance(img, Image.Image): # Save the picture file_path = root_path + '/test.png' img.save(file_path) # Get file size size = get_size_kb(file_path) if size > img_max_size: print(' Pictures can't be more than {} kb, At present {} kb:'.format(img_max_size, size)) # Read the picture image = get_file_content(file_path) # Identify image content text = ocr_img(image, True) # text = ocr_img(image, False) print(text) # Write clipboard set_clipboard_text(text)if __name__ == '__main__': ocr_clipboard()