Search code examples
pythonimagemagickocrtesseract

Perform Tesseract OCR on entire directory with python


I have Multiple number of Images in directory and want to convert it from images to text files. I have to do it manually in terminal one by one. which is headache process. SO, my question is how can i run my code on that folder which contain images.

Here is the terminal command to convert it into text :

convert captcha.png -resize 200% -type Grayscale input.tif  #instead of input.tif i want same file name so, i can recognise easily 

tesseract -l eng input.tif output  #output name as same as file name

Solution

  • Simpler method, no Python, just using two terminal commands, one that converts all the files to TIFF, and one that calls tesseract on each TIFF file:

    1. Convert all your images to TIFF

      convert '*.png' -resize 200% -type Grayscale +adjoin -set filename:name "%t" '%[filename:name].tif'
      
    2. Call tesseract on the results:

      for f in *.tif;do tesseract -l eng "$f" "$(basename "$f" .tif).txt";done
      

      (you can skip the double quotes (") if there are no spaces in your file names.

    Replacement for step 1) if output names look weird:

    for f in *.png;do convert "$f" -resize 200% -type Grayscale "$(basename "$f" .png).tif";done