How to extract text from an image via the CLI (#snippet)

I came across a post by Simon Willison that described how to use the tesseract CLI command to extract text from an image. I took the opportunity to fiddle around with some shell scripting and added an extract-text-from-image command…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

I came across a post by Simon Willison that described how to use the tesseract CLI command to extract text from an image. I took the opportunity to fiddle around with some shell scripting and added an extract-text-from-image command to my dotfiles. It uses tesseract to analyze an image, creates a txt file with the embedded text, copies the ebedded text to the clipboard, and deletes the txt file again.

The tesseract command was available on my machine. If it's not available on yours, install it via your favorite package manager.

Find extract-image-from-texts last iteration below:

function extract-text-from-image() {
  if [ $# -eq 0 ]; then
    echo "Please specify the file you want to scan.";
    echo "  -> extract-text-from-image /some/path/image.png";
    return 1;
  fi

  TARGET_DIR=$(dirname "$1");
  FILENAME=$(basename -- "$1");
  FILENAME_WITHOUT_EXTENSION="${FILENAME%.*}";

  tesseract "$1" "$TARGET_DIR/$FILENAME_WITHOUT_EXTENSION" -l eng txt || return 1;
  pbcopy < "$TARGET_DIR/$FILENAME_WITHOUT_EXTENSION.txt";
  rm "$TARGET_DIR/$FILENAME_WITHOUT_EXTENSION.txt";
  echo "? Text copied to clipboard!";
}

I don't think I'll use this command daily, but I'm amazed by this piece of CLI magic.

Edit: Wesley Martin shared the macOCR project with me and it's also worth a look!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2021-08-01T19:00:00+00:00) How to extract text from an image via the CLI (#snippet). Retrieved from https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/

MLA
" » How to extract text from an image via the CLI (#snippet)." Stefan Judis | Sciencx - Sunday August 1, 2021, https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/
HARVARD
Stefan Judis | Sciencx Sunday August 1, 2021 » How to extract text from an image via the CLI (#snippet)., viewed ,<https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/>
VANCOUVER
Stefan Judis | Sciencx - » How to extract text from an image via the CLI (#snippet). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/
CHICAGO
" » How to extract text from an image via the CLI (#snippet)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/
IEEE
" » How to extract text from an image via the CLI (#snippet)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/. [Accessed: ]
rf:citation
» How to extract text from an image via the CLI (#snippet) | Stefan Judis | Sciencx | https://www.scien.cx/2021/08/01/how-to-extract-text-from-an-image-via-the-cli-snippet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.