使用pytesseract处理手机截图PNG时出现OpenCV TypeError错误求助
问题:手机截图PNG文件OCR识别崩溃,JPG/部分PNG正常
原代码
img = cv2.imread('test.png',cv2.COLOR_BGR2GRAY) custom_config = r'--oem 3 --psm 6' data=pytesseract.image_to_string(img, config=custom_config) print(data)
错误信息
Traceback (most recent call last): File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/PIL/Image.py", line 2992, in fromarray mode, rawmode = _fromarray_typemap[typekey] KeyError: ((1, 1, 3), '<u2') The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/hkc/Documents/work/opencv/test.py", line 13, in <module> data=pytesseract.image_to_string(img, config=custom_config) File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/pytesseract/pytesseract.py", line 423, in image_to_string return { File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/pytesseract/pytesseract.py", line 426, in <lambda> Output.STRING: lambda: run_and_get_output(*args), File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/pytesseract/pytesseract.py", line 277, in run_and_get_output with save(image) as (temp_name, input_filename): File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__ return next(self.gen) File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/pytesseract/pytesseract.py", line 197, in save image, extension = prepare(image) File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/pytesseract/pytesseract.py", line 171, in prepare image = Image.fromarray(image) File "/home/hkc/Documents/work/opencv/cv/lib/python3.10/site-packages/PIL/Image.py", line 2994, in fromarray raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e TypeError: Cannot handle this data type: (1, 1, 3), <u2
错误原因
- 读取参数错误:
cv2.imread的第二个参数应为读取模式,你误用了色彩转换常量cv2.COLOR_BGR2GRAY,导致图像读取格式异常。 - 数据类型不兼容:部分手机截图PNG是16位色深(
<u2代表无符号16位整数),而PIL的Image.fromarray无法直接处理该格式,pytesseract依赖PIL处理图像时触发错误。
修复代码
方案一:直接读取灰度图并处理16位图像
import cv2 import pytesseract # 用正确的灰度模式读取图像 img = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE) # 判断是否为16位图像,转换为8位适配PIL if img.dtype == 'uint16': img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) custom_config = r'--oem 3 --psm 6' data = pytesseract.image_to_string(img, config=custom_config) print(data)
方案二:读取彩色图后转灰度并处理16位图像
import cv2 import pytesseract # 读取彩色图像 img = cv2.imread('test.png') # 16位转8位 if img.dtype == 'uint16': img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) # 转换为灰度图 gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) custom_config = r'--oem 3 --psm 6' data = pytesseract.image_to_string(gray_img, config=custom_config) print(data)
关键说明
cv2.IMREAD_GRAYSCALE:正确的灰度图读取模式,确保输出单通道灰度图像。cv2.normalize:将16位图像的像素值范围(0-65535)映射到8位的0-255,让PIL能正常解析图像数据。
内容的提问来源于stack exchange,提问作者Hemant Kumar Chaudhary
相关产品推荐
相关产品推荐

