+ # Check that it's actually a PNG to avoid problems when loading it
+ # later.
+ #
+ # Each PNG file starts with a 8 byte signature that should be followed
+ # by IHDR chunk which is always 13 bytes in length so the first 16
+ # bytes are fixed (or at least we expect them to be).
+ if bytes[0:16].tostring() != '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR':
+ print '"%s" doesn\'t seem to be a valid PNG file.' % filename
+ continue
+
+ # Try to naively get its size if necessary
+ if with_size:
+ def getInt(start):
+ """ Convert 4 bytes in network byte order to an integer. """
+ return 16777216*bytes[start] + \
+ 65536*bytes[start+1] + \
+ 256*bytes[start+2] + \
+ bytes[start+3];
+
+ size_suffix = "_%dx%d" % (getInt(16), getInt(20))
+