from PIL import Image

filename = "onix_bvd_zigzag"
img = Image.open(filename + ".png")

width, height = img.size
img_decoded = Image.new("RGB", (int(width), int(height)))

def entrelacement (x, y, dim):
    bits = len("{:b}".format(dim-1))
    x = "{coord:0{bits}b}".format(bits=bits, coord=x)
    y = "{coord:0{bits}b}".format(bits=bits, coord=y)
    res = ""
    for i in range(bits):
        res = res + y[i] + x[i]
    # print(bits, x, y, res)
    res = int(res, 2)
    return (res%dim, res//dim)

# print(entrelacement(2,4,width))

for y in range(height):
    for x in range(width):
        img_decoded.putpixel(entrelacement(x,y,width), img.getpixel((x, y)))

img_decoded.save(filename + "_decoded.png")
img_decoded.show()

