PNG for Dummies

This article is to discuss about “PNG for Dummies”

I have two PNG file:
redPixel.png
greenPixel.png

Let’s see
# hexdump -C redPixel.png

00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
00000010  00 00 00 01 00 00 00 01  08 06 00 00 00 1f 15 c4  |................|
00000020  89 00 00 00 04 73 42 49  54 08 08 08 08 7c 08 64  |.....sBIT....|.d|
00000030  88 00 00 00 09 70 48 59  73 00 00 0d d7 00 00 0d  |.....pHYs.......|
00000040  d7 01 42 28 9b 78 00 00  00 19 74 45 58 74 53 6f  |..B(.x....tEXtSo|
00000050  66 74 77 61 72 65 00 77  77 77 2e 69 6e 6b 73 63  |ftware.www.inksc|
00000060  61 70 65 2e 6f 72 67 9b  ee 3c 1a 00 00 00 0d 49  |ape.org..<.....I|
00000070  44 41 54 08 99 63 f8 cf  c0 f0 1f 00 05 00 01 ff  |DAT..c..........|
00000080  ab ce 36 89 00 00 00 00  49 45 4e 44 ae 42 60 82  |..6.....IEND.B`.|
00000090

First 8 bytes is magic numbers.

89 50 4e 47 0d 0a 1a 0a

Stop, let’s see another PNG file.

# hexdump -C greenPixel.png

00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
00000010  00 00 00 01 00 00 00 01  08 06 00 00 00 1f 15 c4  |................|
00000020  89 00 00 00 04 73 42 49  54 08 08 08 08 7c 08 64  |.....sBIT....|.d|
00000030  88 00 00 00 09 70 48 59  73 00 00 0d d7 00 00 0d  |.....pHYs.......|
00000040  d7 01 42 28 9b 78 00 00  00 19 74 45 58 74 53 6f  |..B(.x....tEXtSo|
00000050  66 74 77 61 72 65 00 77  77 77 2e 69 6e 6b 73 63  |ftware.www.inksc|
00000060  61 70 65 2e 6f 72 67 9b  ee 3c 1a 00 00 00 0d 49  |ape.org..<.....I|
00000070  44 41 54 08 99 63 60 68  60 f8 0f 00 02 84 01 80  |DAT..c`h`.......|
00000080  d8 52 5d 89 00 00 00 00  49 45 4e 44 ae 42 60 82  |.R].....IEND.B`.|
00000090

Yeah, we see the different.

Still confused?

Install PyPNG

pip install pypng

Then play this code:

import sys
import png

filenames = sys.argv[1]
img = png.Reader(filenames)

for i in xrange(19):
	try:
		print img.chunk()
	except:
		pass

See the results.

# python readPNG.py redPixel.png

('IHDR', '\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00')
('sBIT', '\x08\x08\x08\x08')
('pHYs', '\x00\x00\r\xd7\x00\x00\r\xd7\x01')
('tEXt', 'Software\x00www.inkscape.org')
('IDAT', '\x08\x99c\xf8\xcf\xc0\xf0\x1f\x00\x05\x00\x01\xff')
('IEND', '')

# python readPNG.py greenPixel.png

('IHDR', '\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00')
('sBIT', '\x08\x08\x08\x08')
('pHYs', '\x00\x00\r\xd7\x00\x00\r\xd7\x01')
('tEXt', 'Software\x00www.inkscape.org')
('IDAT', '\x08\x99c`h`\xf8\x0f\x00\x02\x84\x01\x80')
('IEND', '')

Clear?

redPixel.png

(‘IDAT’, ‘\x08\x99c\xf8\xcf\xc0\xf0\x1f\x00\x05\x00\x01\xff’)

greenPixel.png

(‘IDAT’, ‘\x08\x99c`h`\xf8\x0f\x00\x02\x84\x01\x80’)

Enough for today, thank you for reading this article.

Hope it is useful

Talk Less Code More

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s