import imghdr
import struct


SIZE = {}


def size(fname):
    t = imghdr.what(fname)
    if t:
        with open(fname, 'rb') as fin:
            return dict(SIZE[t](fin), type=t)


def size_png(fin):
    fin.read(8)  # Skip the Magick Numbers
    # First chunk MUST be IHDR
    clen, ctype = struct.unpack('!I4s', fin.read(8))
    assert ctype == 'IHDR'
    data = fin.read(clen)
    fin.read(4)  # CRC
    w, h, d, ct, comp, filt, ilace = struct.unpack('!IIccccc', data)
    return {
        'width': w,
        'height': h,
        'depth': ord(d),
    }

SIZE['png'] = size_png


def size_jpeg(fin):
    soi = fin.read(2)
    assert soi == '\xff\xd8'
    while True:  # Find the SOFx block
        mkr, ll = struct.unpack('!2sH', fin.read(4))
        assert mkr[0] == '\xff'
        data = fin.read(ll - 2)
        if mkr[1] not in ('\xc0', '\xc1', '\xc2', '\xc3', '\xc5', '\xc6',
                          '\xc7', '\xc9', '\xca', '\xcb', '\xcd', '\xce',
                          '\xcf'):
            continue
        bps, height, width = struct.unpack('!cHH', data[:5])
        break
    return {
        'width': width,
        'height': height,
    }

SIZE['jpeg'] = size_jpeg


def size_gif(fin):
    fin.read(6)  # Skip the Magick Numbers
    width, height = struct.unpack('<HH', fin.read(4))
    return {
        'width': width,
        'height': height,
    }

SIZE['gif'] = size_gif

if __name__ == '__main__':
    import sys

    for name in sys.argv[1:]:
        info = size(name)
        if not info:
            print("{}: Unknown".format(name))
        else:
            print("{}: {} {} x {} ({})".format(name,
                                               info['type'],
                                               info['width'],
                                               info['height'],
                                               info))

@@ -144,443 +144,94 @@
     
-return dict(SIZE%5Bt%5D(fin), type=t)%0A%0A%0Adef size_png(fin):%0A    fin.read(8)  # Skip the Magick Numbers%0A    # First chunk MUST be IHDR%0A    clen, ctype = struct.unpack('!I4s', fin.read(8))%0A    assert ctype == 'IHDR'%0A    data = fin.read(clen)%0A    fin.read(4)  # CRC%0A    w, h, d, ct, comp, filt, ilace = struct.unpack('!IIccccc', data)%0A    return %7B%0A        'width': w,%0A        'height': h,%0A        'depth': ord(d),%0A    %7D%0A%0ASIZE%5B'png'%5D = size_png
+test = SIZE.get(t)%0A            if test:%0A                return dict(test(fin), type=t)
 %0A%0A%0Ad
@@ -845,207 +845,966 @@
 ize_
-gif(fin):%0A    fin.read(6)  # Skip the Magick Numbers%0A    width, height = struct.unpack('%3CHH', fin.read(4))%0A    return %7B%0A        'width': width,%0A        'height': height,%0A    %7D%0A%0ASIZE%5B'gif'%5D = size_gif
+png(fin):%0A    fin.read(8)  # Skip the Magick Numbers%0A    # First chunk MUST be IHDR%0A    clen, ctype = struct.unpack('!I4s', fin.read(8))%0A    assert ctype == 'IHDR'%0A    data = fin.read(clen)%0A    fin.read(4)  # CRC%0A    w, h, d, ct, comp, filt, ilace = struct.unpack('!IIccccc', data)%0A    return %7B%0A        'width': w,%0A        'height': h,%0A        'depth': ord(d),%0A    %7D%0A%0ASIZE%5B'png'%5D = size_png%0A%0A%0Adef size_gif(fin):%0A    fin.read(6)  # Skip the Magick Numbers%0A    width, height = struct.unpack('%3CHH', fin.read(4))%0A    return %7B%0A        'width': width,%0A        'height': height,%0A    %7D%0A%0ASIZE%5B'gif'%5D = size_gif%0A%0A# TIFF%0A# SGI RGB%0A%0A# PBM%0A# PGM%0A# PPM%0Adef size_pnm(fin):%0A    fin.read(2)  # Skip the Magick Numbers%0A    # Remember to skip lines starting with # after a newline%0A    width = height = 0%0A    # Then read width%3Cs%3Eheight%0A    return %7B'width': width, 'height': height%7D%0A%0ASIZE%5B'pbm'%5D = size_pnm%0ASIZE%5B'pgm'%5D = size_pnm%0ASIZE%5B'ppm'%5D = size_pnm%0A%0A%0A# SUN Raster%0A# XBM%0A# BMP
 %0A%0Aif

Reorder to match imghdr. Start adding pnm