def find_texthwnd area text left top right bottom = win32guiGetClientRecthwnd width = right - left height = bottom - top wDC = win32guiGetWindowDChwnd dcObj = win32uiCreateDCFromHandlew
To pass bytes data to the classification method, you can modify the find_text method as follows:
def find_text(hwnd, area, text):
left, top, right, bottom = win32gui.GetClientRect(hwnd)
width = right - left
height = bottom - top
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY)
bmpinfo = dataBitMap.GetInfo()
bmpstr = dataBitMap.GetBitmapBits(True)
im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1)
ocr = ddddocr.DdddOcr()
# Convert image to bytes
img_byte_arr = io.BytesIO()
im.save(img_byte_arr, format='PNG')
img_bytes = img_byte_arr.getvalue()
res = ocr.classification(img_bytes)
print(res)
win32gui.ReleaseDC(hwnd, wDC)
cDC.DeleteDC()
win32gui.DeleteObject(dataBitMap.GetHandle())
This modified code converts the image im to bytes using the io.BytesIO class and saves it as a PNG format in img_bytes. Then, it passes the img_bytes to the classification method of the ocr object.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/i12t 著作权归作者所有。请勿转载和采集!