from PIL import Image

image = Image.open("image_chiffree_hydra.png")
data = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)

# image = Image.open("image_chiffree_hydra_test.png")
# data = (2, 3, 5, 7)

# image = Image.open("image_chiffree_hydra_test2.png")
# data = (21, 36, 49, 79)

img = list()

#convert image to list of lists
for y in range(sum(data)):
    row = list()
    for x in range(sum(data)):
        row.append(image.getpixel((x,y)))
    img.append(row)

for loop in range(17):
    #reorder rows
    img_new = list()
    offset = sum(data)-data[0]
    for i in range(len(data)):
        img_new = img_new + img[offset:offset+data[i]]
        if i+1<len(data):
            offset = offset - data[i+1]
    img = img_new.copy()
    #reorder columns
    for x in range(sum(data)):
        tmp = list()
        offset = sum(data)-data[0]
        for i in range(len(data)):
            tmp = tmp + img[x][offset:offset+data[i]]
            if i+1<len(data):
                offset = offset - data[i+1]
        img[x] = tmp

#Rebuild image
for y in range(sum(data)):
    for x in range(sum(data)):
        image.putpixel((x, y), img[y][x])

image.save("image_dechiffree_hydra.png")
image.show()

