Skip to content

Instantly share code, notes, and snippets.

@NuarkNoir
Created August 1, 2019 07:32
Show Gist options
  • Save NuarkNoir/ce37eb27e1df5b1f5048996ce44f69b4 to your computer and use it in GitHub Desktop.
Save NuarkNoir/ce37eb27e1df5b1f5048996ce44f69b4 to your computer and use it in GitHub Desktop.
Renames images in current folder into their perceptionhash value - helps in sfinding similar images
import shutil
import os.path
from glob import glob
import imagehash
from PIL import Image
def rename_images(filenames):
print("Hashing...", end="\r")
nnames = dict()
errored_images = []
for i, file in enumerate(filenames):
print("Hashing... Dealing with {} of {}".format(i+1, len(filenames)), " "*40, end="\r")
try:
nnames[file] = imagehash.phash(Image.open(file))
except:
errored_images.append(file)
print("Hashing done.", " "*30)
if len(errored_images) > 0:
print()
print("====================================================")
print("Got some broken images: ")
print("\n".join(errored_images))
print("====================================================")
input("Press enter key to continue...")
print()
print("Renaming...", end=" ")
for i, name in enumerate(nnames):
print("Renaming... Dealing with {} of {}".format(i+1, len(filenames), " "*10), end="\r")
nn = str(nnames[name])
nfn = nn + ".jpg"
if os.path.isfile(nfn):
k = 1
while os.path.isfile(nfn):
nfn = "{}_{}.jpg".format(nn, k)
k += 1
shutil.move(name, nfn)
print("Renaming done.", " "*40)
rename_images(glob("*.*"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment