Okay, I am trying to do sort of simple image encryption for my college project, all I need to do is open an image as a numpy array and increment the pixel values by an integer key, then save it as 16 bit image, then open that Image and. But whenever I increment the pixels the result turns into a deformed image of that only has shades of black, and when I try to get the original image by performing the reverse (decremented the pixels with the same integer key), it results in a full black blank image. The dimensions are correct but all the data seems to be lost(though when I print the matrices the values seem to be correct).
I have tried using Imageio with freeimage plugin, and open cv but nothing seems to work. I am also a noob so I don't know if I am missing something else
from tkinter import filedialog
from tkinter import *
from PIL import Image
import cv2 as cv
import os
import numpy as np
def encrypt(k):
iload = filedialog.askopenfilename(parent=Main,initialdir=os.getcwd(),title="Please select a file:",filetypes = (("PNG files","*.png"),("jpeg files","*.jpg"),("all files","*.*")))
im= cv.imread(iload,cv.IMREAD_UNCHANGED)
im = im.astype(np.uint16)
print("After Open File Type : ",im.dtype)
print("Orinigal Image : ",im)
im = im.tolist()
for l in range(len(im)):
for j in range(len(im[l])):
for i in range(len(im[l][j])):
im[l][j][i]+=k
#im.putdata(npxls)
im=np.array(im).astype(np.uint16)
#imen.show()
print("Encrypted Image : ",im)
#imageio.imwrite("encrypted.png",im,format='PNG-FI')
cv.imwrite("encrypted.png",im)
img=cv.imread("encrypted.png",cv.IMREAD_UNCHANGED | cv.IMREAD_ANYCOLOR | cv.IMREAD_ANYDEPTH)
print("After Encrypting Saved File Type :",img.dtype)
def decrypt(k):
iload = filedialog.askopenfilename(parent=Main,initialdir=os.getcwd(),title="Please select a file:",filetypes = (("PNG files","*.png"),("jpeg files","*.jpg"),("all files","*.*")))
im=cv.imread(iload, cv.IMREAD_UNCHANGED | cv.IMREAD_ANYCOLOR | cv.IMREAD_ANYDEPTH)
print("Original Image : ",im)
print("After Decrypting Image Type: ",im.dtype)
im = im.astype(np.uint16)
im = im.tolist()
for l in range(len(im)):
for j in range(len(im[l])):
for i in range(len(im[l][j])):
im[l][j][i]-=k
im=np.array(im).astype(np.uint16)
#imen.show()
print("Decrypted Image : ",im)
cv.imwrite("decrypted.png",im,[CV_LOAD_IMAGE_ANYDEPTH ])
img=cv.imread("decrypted.png", cv.IMREAD_ANYCOLOR | cv.IMREAD_ANYDEPTH)
print("After Decrypting Saved Image type: ",img.dtype)
I just need the Image to get changed by adding a key to the pixel values in way that I can change it back with the reverse operation.