-1

I am working on encryption in which the problem says:

For each pixel of the image, convert the pixel decimal value into binary 8 bits. Then separate the MSB and LSB of each 8-bit pixel and convert that into decimal.

I have converted the image into an 8-bit binary for each pixel. Now I don't how to separate the MSB and LSB.

Here is my code:

#taking input image
input = cv2.imread('test.webp')
#print("before\n\n")
cv2.imshow('image before',input)


#Get input size converting to a fixed size

height, width = input.shape\[:2\]

#Desired "pixelated" size

w, h = (256, 256)

#Resize input to "pixelated" size

temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)

print(temp)
#Initialize output image

#the converted pixed into 8bit image
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)
cv2.imwrite("pixelate.jpg", output)
#print(output)
#output
print('after\n\n')
cv2.imshow('image after',output)



cv2.waitKey(0)

cv2.destroyAllWindows()][1]
beaker
  • 16,331
  • 3
  • 32
  • 49

1 Answers1

1

First, read image and print its datatype

img = cv2.imread('test.webp')
print(img.dtype)

If datatype is not uint8, then scale your image to 0-255 and change its datatype to uint8.

Now, mask the 1st (LSB) and last (MSB) bit of each pixel

import numpy as np
mask = np.full(img.shape, 0x7E, dtype=np.uint8)

res = np.bitwise_and(img, mask)

import matplotlib.pyplot as plt
plt.imshow(res)
user8190410
  • 1,264
  • 2
  • 8
  • 15