Last active
February 23, 2016 22:24
-
-
Save theonlypwner/f8e0a7df5fd70053217b to your computer and use it in GitHub Desktop.
Maximum (sized) PNG generator
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 | |
# Maximum PNG generator | |
__copyright__ = "Copyright (C) 2016 Victor Zheng" | |
__licence__ = "GNU GPL v3" | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# Settings | |
OUTPUT_FILE = "max.png" # Do I really need to explain this? | |
IMAGE_W, IMAGE_H = 0x7FFFFFFF, 0x7FFFFFFF # image size [pixels] | |
DEPTH = 4 | |
# Color depth [bytes/pixel] | |
# 1 Grayscale | |
# 3 RGB | |
# 4 RGBA | |
# Script | |
import struct | |
import zlib | |
def B(x): | |
return struct.pack(">B", x & 0xFF) | |
def I4(x): | |
return struct.pack(">I", x & 0xFFFFFFFF) | |
pixels = b"" # too lazy... | |
def writePNG(): | |
width = IMAGE_W | |
height = IMAGE_H | |
bitdepth = 16 # [bit/sample] | |
if DEPTH == 1: | |
colortype = 0 | |
elif DEPTH == 3: | |
colortype = 2 | |
elif DEPTH == 4: | |
colortype = 6 | |
else: | |
raise NotImplementedError("Unsupported color depth " + DEPTH) | |
compresstype = 0 # DEFLATE (only supported method) | |
filtermethod = 0 # adaptive (only supported method) | |
interlaced = 0 # no | |
with open(OUTPUT_FILE, "wb") as f: | |
# Signature | |
f.write(b"\x89PNG\r\n\x1A\n") | |
# IHDR | |
IHDR = b"IHDR" \ | |
+ I4(width) \ | |
+ I4(height) \ | |
+ B(bitdepth) \ | |
+ B(colortype) \ | |
+ B(compresstype) \ | |
+ B(filtermethod) \ | |
+ B(interlaced) | |
f.write(I4(len(IHDR) - 4)) | |
f.write(IHDR) | |
f.write(I4(zlib.crc32(IHDR))) | |
# IDAT | |
compressor = zlib.compressobj(9) # max compression | |
IDAT = b"IDAT" + \ | |
compressor.compress(pixels) + \ | |
compressor.flush() | |
f.write(I4(len(IDAT) - 4)) | |
f.write(IDAT) | |
f.write(I4(zlib.crc32(IDAT))) | |
# IEND | |
# 0x00000000 + "IEND" + CRC32("IEND") | |
f.write(b"\0\0\0\0IEND\xae\x42\x60\x82") | |
if __name__ == '__main__': | |
writePNG() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment