All pastes #2051853 Raw Edit

Stuff

public python v1 · immutable
#2051853 ·published 2011-04-29 08:18 UTC
rendered paste body
import Image    # Should be 'from PIL import Image' - cygwin not so happyimport sysimport struct# IMAGE FORMAT DETAILS#   struct header {#       unsigned long unknown; // always = 1 so far#       unsigned long filesize;#       unsigned long width;#       unsigned long height;#   }#   uint16_t pixel_data[width*height];##   pixel data is packed RGB data##   MSB             LSB#   1 RRRRR GGGGG BBBBB##   High bit unused? Maybe mask?#def decodeFile(filename):    d = open(filename).read()    # Read the image header    unknown, imagesize, width, height = struct.unpack(">IIII", d[:16])    if unknown != 1:        print "Only tested samples have the first field set to '1' - check imag e format"        return None    if imagesize != len(d):        print "Error - filesize in header did not match image size"        return None    imagedata = d[16:]    # sanity check    assert len(imagedata) == height*width*2    a = Image.new("RGB", (width,height) )    pixd = a.load()    for y in xrange(0, height):        for x in xrange(0, width):            index = (y*width + x) * 2            pix = imagedata[index:index+2]            # 555 rgb            ph, = struct.unpack(">H", pix)            r = ((ph >> 10) & 0x1F) << 3            g = ((ph >>  5) & 0x1F) << 3            b = ((ph >>  0) & 0x1F) << 3                        # Image is flipped vertically [like BMP]            pixd[x, height-y-1] = (r,g,b)    return adef main():    name = sys.argv[1]    s = decodeFile(name)    if s:        s.save(name+".png", "png")if __name__ == "__main__":    main()