#!/usr/bin/env python # coding: utf-8 # # Video of this screencast: https://vimeo.com/57296525 # # import os import random from PIL import Image as pImage import numpy BLOCK_SIZE = 20 THRESHOLD = 60 def image_data(filename): """ Get data from image ready for comparison """ filename = filename img = pImage.open(filename) small = img.resize( (BLOCK_SIZE, BLOCK_SIZE), pImage.BILINEAR ) t_data = numpy.array( [sum(list(x)) for x in small.getdata()] ) del img, small return filename, t_data def distance(data1, data2): """ Logical distance between two images on a scale 0..400 """ return sum(1 for x in data1 - data2 if abs(x) > THRESHOLD) def html(dirname): """ Generates HTML from a set of images in a directory. All files must be *.jpg. """ images = \ [image_data(os.path.join(dirname, filename)) \ for filename in os.listdir(dirname) if filename.endswith('.jpg')] # random.shuffle(images) res = [''] for img in images: distances = sorted([ (distance(img[1], x[1]), x) for x in images ]) res += [ '' + str(dist) for dist, x in distances if dist < 220] res += ['
'] res += [''] return '\n'.join(res) if __name__ == '__main__': reference = open('reference.html').read() result = html('/home/maniac/Desktop/4554182') + '\n' print(reference == result)