First no error image renamer code

This commit is contained in:
StarShine 2020-09-09 20:06:33 +02:00
commit 130364a708
3 changed files with 84 additions and 0 deletions

66
Imagenamer.py Normal file
View File

@ -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")

17
Setup.bat Normal file
View File

@ -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

1
readme.md Normal file
View File

@ -0,0 +1 @@
This is JUST for windows