First no error image renamer code
This commit is contained in:
commit
130364a708
|
@ -0,0 +1,66 @@
|
|||
try:
|
||||
from PIL import Image
|
||||
except ImportError:
|
||||
import Image
|
||||
import pytesseract
|
||||
import os
|
||||
import string
|
||||
|
||||
namearray = []
|
||||
indexnow = 0
|
||||
|
||||
pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
|
||||
|
||||
def ocr(filename):
|
||||
newname = pytesseract.image_to_string(Image.open(absolutebasepathin+"\\"+filename)) # We'll use Pillow's Image class to open the image and pytesseract to detect the string in the image
|
||||
return newname
|
||||
|
||||
def namecleaner(filename):
|
||||
print("filenameis "+filename)
|
||||
filename = "_".join(filename.split())
|
||||
filename = filename.replace(" ","")
|
||||
filename = filename.replace("\n","")
|
||||
filename = filename.replace("<","")
|
||||
filename = filename.replace(">","")
|
||||
filename = filename.replace(":","")
|
||||
filename = filename.replace("\"","")
|
||||
filename = filename.replace("/","")
|
||||
filename = filename.replace("\\","")
|
||||
filename = filename.replace("|","")
|
||||
filename = filename.replace("?","")
|
||||
filename = filename.replace("*","")
|
||||
filename = filename.replace(".","")
|
||||
outputname = filename+basename[len(basename)-4:len(basename)]
|
||||
print("outputnameis "+outputname)
|
||||
return outputname
|
||||
|
||||
# List all files in a directory using scandir()
|
||||
basepathin = '.\ImagesToConvert'
|
||||
print("basepathin = "+basepathin)
|
||||
absolutebasepathin = os.path.abspath('.\ImagesToConvert')
|
||||
print("absolutebasepathin = "+absolutebasepathin)
|
||||
basepathout = '.\ImageOutput'
|
||||
print("basepathout = "+basepathout)
|
||||
absolutebasepathout = os.path.abspath('.\ImagesOutput')
|
||||
print("absolutebasepathout = "+absolutebasepathout)
|
||||
with os.scandir(basepathin) as entries:
|
||||
for entry in entries:
|
||||
if entry.is_file():
|
||||
namearray.append(entry.name)
|
||||
|
||||
arraylength = len(namearray)
|
||||
print("arraylength = "+str(arraylength))
|
||||
|
||||
while indexnow < arraylength:
|
||||
basename = namearray[indexnow]
|
||||
print("basename = "+basename)
|
||||
newname = ocr(basename)
|
||||
print("newname = "+newname)
|
||||
cleanname = namecleaner(newname)
|
||||
print("cleanname = "+cleanname)
|
||||
os.rename(absolutebasepathin+"\\"+basename, absolutebasepathout+"\\"+cleanname)
|
||||
print(basename+" is now renamed as "+cleanname)
|
||||
indexnow = indexnow + 1
|
||||
|
||||
|
||||
print("All images are given a name")
|
|
@ -0,0 +1,17 @@
|
|||
@echo off
|
||||
|
||||
echo JUST FOR WINDOWS
|
||||
|
||||
echo install python: https://www.python.org/downloads/
|
||||
start https://www.python.org/downloads/
|
||||
|
||||
echo also install tesseract https://tesseract-ocr.github.io/tessdoc/Downloads.html
|
||||
start https://tesseract-ocr.github.io/tessdoc/Downloads.html
|
||||
|
||||
echo wait untill instalation
|
||||
pause
|
||||
|
||||
md ImagesToConvert
|
||||
md ImagesOutput
|
||||
pip install Pillow
|
||||
pip install pytesseract
|
Loading…
Reference in New Issue