Search code examples
pythonimageocrtesseract

Slicing an image into strips for OCR


I am trying to slice a PNG into a series of strips, so I can use tesseract to accurately read each row of a bank statement (if there is something which already reads a bank statement please tell me <3).

I have started by using the image slicer library which only slices into tiles.

import image_slicer
from PIL import Image

pages = convert_from_path('OCRBeebun/Bacon.pdf', 500)
i = 0
firstRun = False

if firstRun:
    for page in pages:

        page.save("img_"+str(i)+'.png','PNG')
        i+=1


image_slicer.slice("img_0.png",14)

Any ideas?


Solution

  • If you open an image using cv2.imread(), then you can slice image just like you slice a string (you also have to import numpy for that), for eg.,

    import numpy
    import cv2
    
    img = cv2.imread('path-to-image.png', cv2.IMREAD_GRAYSCALE) # you can have that flag if u need it tho
    # if you have the coordinates to slice the image, then
    cropped_img = img[height_start:height_end, width_start:width_end]
    # You can also run the above line in a loop to get more than one sliced image
    
    # To save the sliced image
    cv2.imwrite('cropped_image.png', cropped_img)
    

    Enter your values in the height_start, height_end, width_start, & width_end