#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-only """Print textual metadata from a PNG file.""" import struct, sys with open(sys.argv[1], "rb") as source: if source.read(8) != b"\x89PNG\r\n\x1a\n": raise SystemExit("not a PNG") while True: header = source.read(8) if len(header) != 8: raise SystemExit("truncated PNG") length, kind = struct.unpack(">I4s", header) data = source.read(length) if len(data) != length or len(source.read(4)) != 4: raise SystemExit("truncated PNG") if kind == b"tEXt": key, _, value = data.partition(b"\0") print(key.decode("latin-1") + "=" + value.decode("latin-1")) if kind == b"IEND": break