commit 130364a7085a4f6a13fc1dc6c8ef44cb9b7fc0a2 Author: StarShine Date: Wed Sep 9 20:06:33 2020 +0200 First no error image renamer code diff --git a/Imagenamer.py b/Imagenamer.py new file mode 100644 index 0000000..9373eef --- /dev/null +++ b/Imagenamer.py @@ -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") diff --git a/Setup.bat b/Setup.bat new file mode 100644 index 0000000..4b03cbb --- /dev/null +++ b/Setup.bat @@ -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 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..67678cb --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +This is JUST for windows \ No newline at end of file