Last active
December 11, 2015 20:59
-
-
Save cgoldberg/4658936 to your computer and use it in GitHub Desktop.
utilities for analyzing PNG image files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import struct | |
def get_image_info(data): | |
if is_png(data): | |
w, h = struct.unpack('>LL', data[16:24]) | |
width = int(w) | |
height = int(h) | |
else: | |
raise Exception('not a png image') | |
return width, height | |
def is_png(data): | |
return (data[:8] == '\211PNG\r\n\032\n'and (data[12:16] == 'IHDR')) | |
if __name__ == '__main__': | |
with open('foo.png', 'rb') as f: | |
data = f.read() | |
print is_png(data) | |
print get_image_info(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment