Created
July 19, 2017 06:26
-
-
Save pyokagan/4d1fb7824d8a043c77a73ee949463e2d to your computer and use it in GitHub Desktop.
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
import struct | |
import binascii | |
import zlib | |
f = open('out.png', 'wb') | |
# Write PNG header | |
f.write(bytes([137, 80, 78, 71, 13, 10, 26, 10])) | |
# Write IHDR chunk | |
f.write(struct.pack('!I', 13)) # Chunk size | |
data = b'IHDR' + struct.pack('!IIBBBBB', 2, 2, 8, 6, 0, 0, 0) | |
f.write(data) | |
f.write(struct.pack('!I', binascii.crc32(data))) | |
# Write IDAT data | |
pixels = bytes([ 0, 255, 255, 255, 255, 0, 0, 0, 255, | |
0, 100, 42, 73, 255, 162, 255, 9, 255 ]) | |
pixels = zlib.compress(pixels) | |
data = b'IDAT' + pixels | |
f.write(struct.pack('!I', len(pixels))) # Chunk size | |
f.write(data) | |
f.write(struct.pack('!I', binascii.crc32(data))) | |
# Write IEND | |
f.write(struct.pack('!I', 0)) | |
data = b'IEND' | |
f.write(data) | |
f.write(struct.pack('!I', binascii.crc32(data))) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment