Deep Filter
Challenge
Solution
import struct
import zlib
from PIL import Image
from bitarray import bitarray
path = "logo.png"
img = Image.open(path) # Pour récupérer les infos du PNG
png = open(path, "rb").read() # Pour lire le PNG en brut
# Parse des chunks
i = 8
idat = b""
while i < len(png):
length = struct.unpack(">I", png[i:i+4])[0]
ctype = png[i+4:i+8]
data = png[i+8:i+8+length]
i += 12 + length
if ctype == b"IDAT":
idat += data
if ctype == b"IEND":
break
raw = zlib.decompress(idat)
# Octets par ligne = pixels * canaux (RGB) + 1 (filtre)
rowlen = 1 + img.width * len(img.getbands())
# Le premier octet est le filtre
filters = [raw[r*rowlen] for r in range(img.height)]
# On map: 1 -> 0 et 2 -> 1
bits = [(0 if f == 1 else 1) for f in filters]
# Transformation bits -> bytes
print(bitarray(bits).tobytes().strip().decode())Mis à jour