I have a rectangle image with a number in it. The position of the number can vary in the image. I try to OCR the number and want to crop everything else except the number. What would be the easiest way to do this?
0
Try to use method of getbbox to get the area only for the number. Here's a outbox after find edge, so removed it to help find the area for the number.
from PIL import Image, ImageFilter
im = Image.open("D:/8.png")
new_im = im.filter(ImageFilter.FIND_EDGES)
w, h = im.size
d = 5
new_im = new_im.crop((d, d, w-d, h-d)) # Remove outline box
l, t, r, b = new_im.convert('1').getbbox()
char_im = im.crop((l+d, t+d, r+d, b+d))
char_im.show()