Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Created November 21, 2022 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remoharsono/2518788e925663a5bf60fab02e6c079d to your computer and use it in GitHub Desktop.
Save remoharsono/2518788e925663a5bf60fab02e6c079d to your computer and use it in GitHub Desktop.
Crop image and extract text from cropped image
from PIL import Image
from pytesseract import pytesseract
path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
pytesseract.tesseract_cmd = path_to_tesseract
img = Image.open("original_image.png")
# img.show()
box = (297, 10, 584, 50)
img2 = img.crop(box)
croppedImage = "cropped_image.png"
img2.save(croppedImage)
# img2.show()
img3 = Image.open(croppedImage)
text = pytesseract.image_to_string(img3)
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment