[Image-SIG] PNG Pixels to Hex/Binary

Karsten Hiddemann karsten.hiddemann at mathematik.uni-dortmund.de
Mon May 26 23:54:24 CEST 2008

Rodrigo schrieb:
> I’ve been trying to convert a line of pixels in PNG format, into a 
> binary file.  So if the first pixel is #ffff12 it will write the first 
> part of the filename.txt ffff12. if second pixel is #15dc3f it will 
> write the next as 15dc3f.  So if I do a hex edit of filename.txt it will 
> show “ffff1215dc3f”.  Would appreciate your help in code for this.

from PIL import Image
import sys

if __name__=="__main__":
		im = Image.open("image.png")
		for p in im.getdata():
			sys.stdout.write("%02x%02x%02x" % p)

More information about the Image-SIG mailing list