def get_png_size(filepath):
    pngfile = file(filepath, 'rb')
    signature = pngfile.read(8)
    if (signature != struct.pack("8B", 137, 80, 78, 71, 13, 10, 26, 10)):
        raise ValueError("Invalid PNG signature")
    while True:
        try:
            tag, data = read_png_chunk(pngfile)
        except __HOLE__, e:
            raise ValueError('Invalid PNG file: ' + e.args[0])
        if tag == 'IHDR': # http://www.w3.org/TR/PNG/#11IHDR
            return struct.unpack("!2I5B", data)[:2]
    raise ValueError('PNG header not found')
dataset/ETHPy150Open akaihola/django-cairo-text/cairotext/templatetags/cairotext.py/get_png_size