From f20e48851813108139c1068a95a209d2e75da349 Mon Sep 17 00:00:00 2001 From: StarShine Date: Sun, 1 Nov 2020 13:36:11 +0100 Subject: [PATCH] Now images with no readable text are renamed with an UUID !! --- Imagenamer.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Imagenamer.py b/Imagenamer.py index d7fc8af..9a67a73 100644 --- a/Imagenamer.py +++ b/Imagenamer.py @@ -8,6 +8,7 @@ except ImportError: import pytesseract import os import string +import uuid namearray = [] indexnow = 0 @@ -36,8 +37,7 @@ def namecleaner(filename): filename = filename.replace("?","") filename = filename.replace("*","") ################################## - filename = filename.replace(".","") # Get rid of extra dots - outputname = filename+basename[len(basename)-4:len(basename)] # Get the extension of the file + outputname = filename.replace(".","") # Get rid of extra dots print("outputnameis "+outputname) return outputname @@ -55,7 +55,7 @@ absolutebasepathout = os.path.abspath('.\ImagesOutput') print("absolutebasepathout = "+absolutebasepathout) # List all files in a directory using scandir() -with os.scandir(basepathin) as entries: +with os.scandir(absolutebasepathin) as entries: for entry in entries: if entry.is_file(): # Fill an array with the list @@ -75,9 +75,18 @@ while indexnow < arraylength: # Call "namecleaner" to get rid of forbiden characters, line breaks and spaces. cleanname = namecleaner(newname) print("cleanname = "+cleanname) - os.rename(absolutebasepathin+"\\"+basename, absolutebasepathout+"\\"+cleanname) - print(basename+" is now renamed as "+cleanname) - indexnow = indexnow + 1 + if cleanname != "": + cleanname = cleanname + basename[len(basename)-4:len(basename)] + os.rename(absolutebasepathin+"\\"+basename, absolutebasepathout+"\\"+cleanname) + print(basename+" is now renamed as "+cleanname) + else: + UUIDnow = str(uuid.uuid4()) + cleanname = namecleaner(UUIDnow) + cleanname = cleanname + basename[len(basename)-4:len(basename)] + os.rename(absolutebasepathin+"\\"+basename, absolutebasepathout+"\\"+cleanname) + print(basename+" is now "+cleanname) + + indexnow += 1 print("All images are given a name")