User:Manetta/prototyping/python-pil-brightness-sorting
using python & PIL, re-sorting on brightness:
from PIL import Image
import os, shutil
from glob import glob
# ***FROM VIDEO TO IMAGE***
inputfilename = "2001"
extension = ".mp4"
cmd = 'ffmpeg -i "{0}{1}" -f image2 -r .01 -an -y output/frame%04d.jpg'
cmd = cmd.format(inputfilename, extension)
print cmd
os.system(cmd)
# ***SORTING IMAGES ON BRIGHTNESS (make grayscale, convert to 1x1px image, value the grayscaleness of this pixel) ***
def avg(p):
pil_im = Image.open(p)
i=pil_im.convert('L')
i.thumbnail((1,1))
g=i.getpixel((0,0))
return g
imgs = glob("output/*.jpg")
imgs.sort()
d = []
for x in imgs:
d.append((avg(x), x))
d.sort()
print d
i=0
for g, image in d:
newname='sorted/sorted{0:04d}.jpg'.format(i)
print "copying", image , "to", newname
shutil.copyfile(image, newname)
i=i+1
# ***FROM IMAGES TO VIDEO***
cmd = "ffmpeg -f image2 -r 15 -i sorted/sorted%04d.jpg -y export-{0}.gif"
cmd = cmd.format(inputfilename)
print cmd
os.system(cmd)