Created
August 1, 2019 07:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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