Issue
Using PIL Image to binarize an image to optimize OCR shows good results on-screen but saves all black PNG. And when passing the image object to process with pytesseract it also returns no text...
Note: I don't really need to save the processed image as long as the OCR works.
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)
OUTPUTS:
print(len(img_lines)) -> 1
print(img_lines) -> <0x0c>
Solution
You need to convert the image mode to RGB:
if img.mode != 'RGB':
img = img.convert('RGB')
img.save(basepath+'processed.png')
That's because the save
method assumes RGB mode.
Answered By - Amin Gheibi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.