import struct import zlib from io import BytesIO from PIL import Image from pictures import SIGNATURE, chunk PASSES = ( (0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2), ) def boundary_png(depth: int, transparent: tuple[int, ...]) -> bytes: width, height = 8000, 5000 neighbor = (transparent[0] ^ 1, *transparent[1:]) row = b"\x00" + packed(list(transparent) * (width // 2) + list(neighbor) * (width // 2), depth) compressor = zlib.compressobj() payload = b"".join(compressor.compress(row) for _ in range(height)) + compressor.flush() header = struct.pack( ">IIBBBBB", width, height, depth, 0 if len(transparent) == 1 else 2, 0, 0, 0 ) return ( SIGNATURE + chunk(b"IHDR", header) + chunk(b"tRNS", b"".join(value.to_bytes(2, "big") for value in transparent)) + chunk(b"IDAT", payload) + chunk(b"IEND", b"") ) def packed(samples: list[int], depth: int) -> bytes: if depth >= 8: return b"".join(value.to_bytes(depth // 8, "big") for value in samples) per_byte = 8 // depth padded = samples + [0] * (-len(samples) % per_byte) return bytes( sum(value << (8 - depth * (i + 1)) for i, value in enumerate(padded[j : j + per_byte])) for j in range(0, len(padded), per_byte) ) def filtered(row: bytes, previous: bytes, bpp: int, method: int) -> bytes: result = bytearray([method]) for i, value in enumerate(row): left = row[i - bpp] if i >= bpp else 0 above = previous[i] corner = previous[i - bpp] if i >= bpp else 0 estimate = left + above - corner paeth = min((left, above, corner), key=lambda candidate: abs(estimate - candidate)) predictor = (0, left, above, (left + above) // 2, paeth)[method] result.append((value - predictor) % 256) return bytes(result) def png_with_trns( depth: int, transparent: tuple[int, ...], *, size: tuple[int, int] = (64, 64), filtering: int = 0, interlace: int = 0, orientation: int = 1, ) -> tuple[bytes, bytes]: width, height = size neighbors = [ tuple(value ^ (1 << bit) if i == channel else value for i, value in enumerate(transparent)) for bit in ([0, 8] if depth == 16 else [0]) for channel in range(len(transparent)) ] neighbors.insert(0, ((transparent[0] + 1) % (1 << depth), *transparent[1:])) pixels = [ [ transparent if x < (width + 1) // 2 else neighbors[(y // 8) % len(neighbors)] for x in range(width) ] for y in range(height) ] raw = bytearray() for x0, y0, dx, dy in PASSES if interlace else ((0, 0, 1, 1),): columns = range(x0, width, dx) if not columns: continue previous = bytes((len(columns) * len(transparent) * depth + 7) // 8) for y in range(y0, height, dy): row = packed([value for x in columns for value in pixels[y][x]], depth) raw.extend(filtered(row, previous, max(1, len(transparent) * depth // 8), filtering)) previous = row header = struct.pack( ">IIBBBBB", width, height, depth, 0 if len(transparent) == 1 else 2, 0, 0, interlace ) exif = Image.Exif() exif[0x0112] = orientation compressed = zlib.compress(raw) data = ( SIGNATURE + chunk(b"IHDR", header) + chunk(b"tRNS", b"".join(value.to_bytes(2, "big") for value in transparent)) + chunk(b"eXIf", exif.tobytes()[6:]) + b"".join(chunk(b"IDAT", compressed[i : i + 7]) for i in range(0, len(compressed), 7)) + chunk(b"IEND", b"") ) expected = bytearray() for row in pixels: for pixel in row: channels = tuple( v >> 8 if depth == 16 else v * 255 // ((1 << depth) - 1) for v in pixel ) expected.extend( (255, 255, 255) if pixel == transparent else channels * (3 if len(channels) == 1 else 1) ) buffer = BytesIO() Image.frombytes("RGB", size, expected).save(buffer, "PNG", exif=exif) return data, buffer.getvalue()