Created
May 27, 2025 06:04
-
-
Save TypeA2/716a496a6cbbbe4e285d0e787a1d2573 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
| #!/usr/bin/env python3 | |
| import sys | |
| import zlib | |
| from pathlib import Path | |
| if len(sys.argv) > 1: | |
| p = Path(sys.argv[1]) | |
| assert p.exists() | |
| instream = p.open("rb") | |
| else: | |
| instream = sys.stdin.buffer | |
| def read(n: int) -> bytes: | |
| global instream | |
| return instream.read(n) | |
| def write(data: bytes): | |
| sys.stdout.buffer.write(data) | |
| png_header = read(8) | |
| assert png_header == b"\x89PNG\x0D\x0A\x1A\x0A" | |
| write(png_header) | |
| idat = b"IDAT" | |
| while True: | |
| size = int.from_bytes(read(4), "big") | |
| chunk = read(4) | |
| data = read(size) | |
| crc = read(4) | |
| if chunk == b"IDAT": | |
| idat += data | |
| elif (chunk == b"sRGB") or (chunk == b"IHDR" and size == 13): | |
| write(size.to_bytes(4, "big") + chunk + data + crc) | |
| if len(chunk) != 4 or len(data) != size or len(crc) != 4: | |
| break | |
| write((len(idat) - 4).to_bytes(4, "big")) | |
| write(idat) | |
| write(zlib.crc32(idat).to_bytes(4, "big")) | |
| write(b"\x00\x00\x00\x00IEND\xAE\x42\x60\x82") | |
| instream.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment