import Image from cStringIO import StringIO import psyco psyco.full() histogram = {} png_header = '\x89PNG' frames = open("allflights.mpng").read().split(png_header) for frame in frames: if not frame: continue # Put the PNG header back on and decompress the PNG im = Image.open(StringIO(png_header + frame)).convert("RGBA") # Plot each red pixel on the histogram for i, pix in enumerate(im.getdata()): if pix == (255, 0, 0, 255): histogram[i] = histogram.get(i, 0) + 1 hist = open("flights.hist", "w") width = im.size[0] for i, count in histogram.items(): y = i / width x = i % width hist.write("%d %d %d\n" % (x, y, count))