0

I am tasked with writing a program that can take an image file as input, then encrypt the image with some secondary code that i have already written, and finally output the encrypted image file.

I would like to import an image, make it a 1d array of numbers, perform some encryption on this 1d array (which will make it into a 2d array which i will flatten), and then be able to output the encrypted 1d array as an image file after converting it back to whatever format it was in on input.

I am wondering how this can be done, and details about what types of image files can be accepted, and what libraries may be required. Thanks

EDIT: this is some code i have used, img_arr stores the image in an array of integers, max 255. This is what i want, however now i require to convert back into the original format, after i have performed some functions on img_arr.

from PIL import Image
    img = Image.open('testimage.jfif')
    print('img first: ',img)
    img = img.tobytes()
    img_arr=[]
    for x in img:
          img_arr.append(x)
    img2=Image.frombytes('RGB',(460,134),img)
    print('img second: ',img2)

my outputs are slightly different

img first: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=460x134 at 0x133C2D6F970>
img second: <PIL.Image.Image image mode=RGB size=460x134 at 0x133C2D49EE0>
bobdaves69
  • 79
  • 1
  • 2
  • 7
  • Use base64. This link answers your question. https://stackoverflow.com/questions/3715493/encoding-an-image-file-with-base64 – Mohammad sadegh borouny Apr 29 '21 at 13:50
  • 1
    @bobdaves69 what have you tried so far? – Bilal Apr 29 '21 at 16:55
  • @Bilal Firstly, i have tried using Pillow. I will edit the question with some code i tried. – bobdaves69 Apr 29 '21 at 16:59
  • 1
    @bobdaves69 you can see this useful [tutorial using OpenCV](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html) – Bilal Apr 29 '21 at 17:49
  • @Bilal thanks. i simply need to read the entire image as an array of numbers, then output the result back to an image, but its turning out to be quite a lot more difficult than expected. I will take a look at the link and come back tomorrow. – bobdaves69 Apr 29 '21 at 18:27

1 Answers1

0

In programming, Base64 is a group of binary-to-text encoding schemes that represent binary data (more specifically, a sequence of 8-bit bytes) in an ASCII string format by translating the data into a radix-64 representation.

Fortunately, you can encode and decode the image binary file with python based on base64. The following link helps you.

Encoding an image file with base64

  • is this the only way, to represent the image as an array of bits? adding base64 encoding will make the program unnecessarily more complicated and time consuming as i will have to decode each time – bobdaves69 Apr 29 '21 at 15:15