Skip to content

Instantly share code, notes, and snippets.

@DimChtz
Last active November 8, 2017 11:00
Show Gist options
  • Select an option

  • Save DimChtz/e53a34e28b6ec2092a4ff361a69986ab to your computer and use it in GitHub Desktop.

Select an option

Save DimChtz/e53a34e28b6ec2092a4ff361a69986ab to your computer and use it in GitHub Desktop.
A little something to remove similar concurrent (only concurrent) images in a folder(s) in Python.
import math, operator
from functools import reduce
from PIL import Image
from os import listdir, remove
from os.path import isfile, join
import sys
def update(currImage, totalImages, suffix=''):
filledSize = int(round(20 * currImage / float(totalImages)))
perc = round(100.0 * currImage / float(totalImages), 1)
bar = '=' * filledSize + '-' * (20 - filledSize)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, perc, '%', suffix))
sys.stdout.flush()
def compare(file1, file2):
h1 = Image.open(file1).histogram()
h2 = Image.open(file2).histogram()
return math.sqrt(reduce(operator.add, map(lambda a,b: (a-b)**2, h1, h2)) / len(h1))
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("Usage: filter.py folder1 [,folder2] [,folderN]")
else:
for path in sys.argv[1:]:
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
for i in range(len(files) - 2, -1, -1):
update(len(files) - i, len(files), files[i])
if (compare(files[i], files[i + 1]) < 150):
remove(files[i])
files.remove(files[i])
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment