Skip to content

Added script to extract text from image directory in 'IMAGES & PHOTO SCRIPTS' #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions IMAGES & PHOTO SCRIPTS/Extract Texts from Image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Extract text from images in a given directory

## Description
This script will extract the text from images in a specified directory and store the output in a given .txt file. The .txt file will contain the text contents of the images in order of their presence in the given directory.

## Requirements

`$ pip install Pillow`
`$ pip install pytesseract`

Download and install the required tesseract.exe file here: https://osdn.net/projects/sfnet_tesseract-ocr-alt/downloads/tesseract-ocr-setup-3.02.02.exe/

## Steps To Execution
- Fork this repo and navigate to Extract Text From Image folder in local folder
- Edit `image-text.py` with the string for the images directory.
- Run this code like so; `$ python image-text.py`
- In a short bit you'd have the .txt file with the texts extracted
- Enjoy and goodluck on your freelancing copy-typing jobs! (how the script idea came to be. Really couldn't type out text in TONS of image files lol)

## Code Output
`"IMAGE_TITLE" done` for each image in directory when text extraction is complete for said image
`Text extract script completed!` - at the end of the script.

Hit `Ctrl-C` to exit script.
36 changes: 36 additions & 0 deletions IMAGES & PHOTO SCRIPTS/Extract Texts from Image/image-text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import pytesseract
import signal
import time
from PIL import Image
from os import closerange

def handler(signum, frame):
print("Text extraction script exited!")
exit(1)

signal.signal(signal.SIGINT, handler)

directory = os.fsencode(r"image files directory")
directory_in_str = r"image files directory"

for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".img") or filename.endswith(".jpeg") or filename.endswith(".jpg"):
image = os.path.join(directory_in_str, filename)

# check Program Files(x86) for tesseract.exe (Windows machines)
pytesseract.pytesseract.tesseract_cmd = r"tesseract.exe directory"

text = pytesseract.image_to_string(Image.open(image), lang="eng")
with open("output.txt", "a", encoding='utf-8') as o:
print(os.path.basename(image) + "\r" + os.path.basename(image) + " done")
o.write('\n\n\n[NEW IMAGE]\n')
o.write(image)
o.write('\n')
o.write(text)
continue
else:
continue

print("Text extract script completed!")