pythonimageimage-processingfeistel-cipher

Encode an image with Feistel cipher and Python


I'm trying to encode an image using the Feistel cipher. The idea was to take an image file, read byte per byte the image, encode every byte with my Feistel cipher, and re-create a new image with the encoded byte generated by the cipher.

Unfortunately, I've found out most of the common image formats use headers that, once encoded, make the encoded image corrupted.

Having a look at the PIL package I've found out the PIL.Image.frombytes function is able to create an image object given a BytesIO object. With the image object, I'm able to recreate the image with the save function

My issue now is, how to open an image and read the actual image payload in bytes that I need to process with my Feistel cipher.

If I use the code

with open('image.jpg', 'rb') as file:
    data = file.read()

I read the whole file, including the headers, which I don't need


Solution

  • The best solution is to encode every pixel of the image by themselves and then re-create the image with the new pixels encoded

    #open image file to process
    im = Image.open(file_name, 'r')
    #get pixel value
    pix_val = list(im.getdata())
    #get dimensions 
    width, height = im.size
    
    tmp = 0
    #create image file processed
    imge = Image.new("RGB", (width, height))
    pix = imge.load()  #load the new pixel color
    
    # create image encoded
    for x in range(height):
      for y in range(width):
        #double loop to encode every pixel
        pix[y,x] = (encode(pix_val[tmp][0]),encode(pix_val[tmp][1]),encode(pix_val[tmp][2]))
        tmp += 1
    

    take note that the function encode should be your Feistel cipher function. In this case, it takes an 8-bit integer and returns an 8-bit integer (which is the representation of each pixel's color but it can be edited according to your needs