Skip to content

Instantly share code, notes, and snippets.

@theonlypwner
Last active February 23, 2016 22:24

Revisions

  1. theonlypwner renamed this gist Feb 23, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes
  2. theonlypwner revised this gist Feb 23, 2016. 1 changed file with 0 additions and 0 deletions.
    Binary file added max.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  3. theonlypwner revised this gist Feb 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion maxpng.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    # Settings

    OUTPUT_FILE = "max.png" # Do I really need to explain this?
    IMAGE_W, IMAGE_H = 0x7FFFFFF, 0x7FFFFFFF # image size [pixels]
    IMAGE_W, IMAGE_H = 0x7FFFFFFF, 0x7FFFFFFF # image size [pixels]
    DEPTH = 4
    # Color depth [bytes/pixel]
    # 1 Grayscale
  4. theonlypwner revised this gist Feb 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion maxpng.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@
    # Settings

    OUTPUT_FILE = "max.png" # Do I really need to explain this?
    IMAGE_W, IMAGE_H = 0x7FFFFFF, 0x7FFFFFF # image size [pixels]
    IMAGE_W, IMAGE_H = 0x7FFFFFF, 0x7FFFFFFF # image size [pixels]
    DEPTH = 4
    # Color depth [bytes/pixel]
    # 1 Grayscale
  5. theonlypwner created this gist Feb 22, 2016.
    86 changes: 86 additions & 0 deletions maxpng.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    #!/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 = 0x7FFFFFF, 0x7FFFFFF # 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()