Skip to content
Advertisement

A copy of image array not working in python

So I am working on Image Encryption and using a chaotic map for diffusion. So I have been trying to rearrange pixels of an image and made a rearranging code. It works fine for normal arrays but when I use it on image, without any rearranging, just a simple copy, the new array doesn’t make any image, in fact np.array_equal prints false. Here is the code snipet:

import numpy as np
import matplotlib.image as img
import matplotlib.pyplot as plt
image = img.imread(r"D:ResearchPythondean.png")
print(image.shape)
height = image.shape[0]
width = image.shape[1]
# height=5
# width=5
encryptedImage2 = np.zeros(shape=image.shape, dtype=np.uint8)
encryptedImage = [[[1,2,3],[6,7,8],[11,12,13],[16,17,18],[21,22,23]],[[4,5,9],[6,7,8],[11,12,13],[16,17,18],[21,22,23]],[[1,2,3],[6,7,8],[11,12,13],[16,17,18],[21,22,23]],[[1,2,3],[6,7,8],[11,12,13],[16,17,18],[21,22,23]],[[1,2,3],[6,7,8],[11,12,13],[16,17,18],[21,22,23]]]

xindex1 = []
yindex1 = []
k=0
for i in range(width):
    xindex1.append(i)
for i in range(height):
    yindex1.append(i)
xindex1[2],xindex1[4]=xindex1[4],xindex1[2]
xindex1[1],xindex1[3]=xindex1[3],xindex1[1]
xindex1[0],xindex1[1]=xindex1[1],xindex1[0]
yindex1[2],yindex1[4]=yindex1[4],yindex1[2]
yindex1[1],yindex1[3]=yindex1[3],yindex1[1]
for i in range(height):
    for j in range(width):
        encryptedImage2[i][j] = image[i][j] 
print(encryptedImage2)
encryptedImage3 = np.zeros(shape=[height, width,3], dtype=np.uint8)
for i in range(height):
    for j in range(width):
        encryptedImage3[yindex1[i]][xindex1[j]] = encryptedImage2[i][j] 
print("Dec")
print(encryptedImage3)
# plt.imshow(image)
# plt.show()
# plt.imshow(encryptedImage2)
# plt.show()
# plt.imshow(encryptedImage3)
# plt.show()


print(np.array_equal(image, encryptedImage2) )

It works just fine for other simple arrays with size(x,y,3) which is the same format image array is stored. How do I make it work?

Advertisement

Answer

Ok after searching for quite some time, and even trying cv2 for imread, i found my working solution to be : replace encryptedImage2 = np.zeros(shape=image.shape, dtype=np.uint8) with encryptedImage2 = np.copy(image)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement