-
-
Save skynode/e26f390f46abf4d0ee727fb08f3c20cc to your computer and use it in GitHub Desktop.
simple tools to write (block) data to png 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 python3 | |
# Distributed under the MIT software license | |
import binascii, struct, sys | |
from PIL import Image | |
def div_roundup(x,y): | |
return (x+y-1)//y | |
f = open(sys.argv[1], 'r') | |
outfilename = sys.argv[2] | |
blockdata = binascii.a2b_hex(f.read().strip()) | |
metaheader = struct.pack('>II', len(blockdata), binascii.crc32(blockdata)) | |
data = metaheader + blockdata | |
pixels=div_roundup(len(data), 4) | |
width=512 # could be made adaptive... | |
height=div_roundup(pixels, width) | |
print('exporting %d×%d to %s' % (width,height,outfilename)) | |
padding_len = width*height*4 - len(data) | |
data += b'\x00' * padding_len | |
img = Image.frombytes("RGBA", (width, height), data) | |
img.save(outfilename) |
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 python3 | |
# Distributed under the MIT software license | |
import binascii, struct, sys | |
from PIL import Image | |
img = Image.open(sys.argv[1]) | |
outfilename = sys.argv[2] | |
assert(img.format == 'PNG' and img.mode == 'RGBA') | |
imgdata = img.tobytes() | |
metaheader = imgdata[0:8] | |
(blocksize,crc) = struct.unpack('>II', metaheader) | |
print('size {:d}×{:d}'.format(img.width, img.height)) | |
print('metaheader:') | |
print(' size : {:d}'.format(blocksize)) | |
print(' CRC32 : {:x}'.format(crc)) | |
assert(8+blocksize <= len(imgdata)) | |
blockdata = imgdata[8:8+blocksize] | |
assert(binascii.crc32(blockdata) == crc) | |
with open(outfilename, 'w') as f: | |
f.write(binascii.b2a_hex(blockdata).decode()) | |
f.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment