PIL图像显示正常但保存黑色
PIL image shows OK but saves black
使用 PIL Image 对图像进行二值化以优化 OCR 在屏幕上显示良好的结果,但保存全黑 PNG。当将图像对象传递给 pytesseract 进行处理时,它也 returns 没有文本...
注意:我真的不需要保存处理后的图像,只要 OCR 有效即可。
basepath = 'img_bin/'
image_list = os.listdir(basepath)
for file in image_list:
if os.path.isfile(os.path.join(basepath, file)):
print(file)
img = Image.open(basepath+file).convert("L")
img = ImageOps.invert(img)
threshold = 20
table = []
pixelArray = img.load()
for y in range(img.size[1]): # binaryzate it
List = []
for x in range(img.size[0]):
if pixelArray[x,y] > threshold:
List.append(0)
else:
List.append(255)
table.append(List)
img = Image.fromarray(np.array(table))
img.show()
img.save(basepath+'processed.png')
img_lines = pytesseract.image_to_string(img)
print(len(img_lines))
print(img_lines)
输出:
print(len(img_lines)) -> 1
print(img_lines) -> <0x0c>
img.show():
已保存 'processed.png'
您需要将图像模式转换为RGB:
if img.mode != 'RGB':
img = img.convert('RGB')
img.save(basepath+'processed.png')
那是因为 save
方法采用 RGB 模式。
使用 PIL Image 对图像进行二值化以优化 OCR 在屏幕上显示良好的结果,但保存全黑 PNG。当将图像对象传递给 pytesseract 进行处理时,它也 returns 没有文本...
注意:我真的不需要保存处理后的图像,只要 OCR 有效即可。
basepath = 'img_bin/'
image_list = os.listdir(basepath)
for file in image_list:
if os.path.isfile(os.path.join(basepath, file)):
print(file)
img = Image.open(basepath+file).convert("L")
img = ImageOps.invert(img)
threshold = 20
table = []
pixelArray = img.load()
for y in range(img.size[1]): # binaryzate it
List = []
for x in range(img.size[0]):
if pixelArray[x,y] > threshold:
List.append(0)
else:
List.append(255)
table.append(List)
img = Image.fromarray(np.array(table))
img.show()
img.save(basepath+'processed.png')
img_lines = pytesseract.image_to_string(img)
print(len(img_lines))
print(img_lines)
输出:
print(len(img_lines)) -> 1
print(img_lines) -> <0x0c>
img.show():
已保存 'processed.png'
您需要将图像模式转换为RGB:
if img.mode != 'RGB':
img = img.convert('RGB')
img.save(basepath+'processed.png')
那是因为 save
方法采用 RGB 模式。