Using OpenCV, I am trying to pass a jpg picture into a numpy array, change some of the pixels and export the changed jpg image with cv2.imwrite and then decode it. This looks like it works as the image file was originally 463kb and after the new file is written the file size is 866kb.
The problem is that the decoder program returns gibberish. I'm not sure how to proceed. Any help would be appreciated.
Example Output::
Code or Decode
decode
What is the location of the Image?
Example format> C:\Users\username\folder\filename.jpg
C:\\Users\\ph1\\Desktop\\coding\\message.jpg
.~>;989;<<=:8;88;>==>@@?>8;;;::998:9;<;;?>=>>@A??<????????>??>>?@>??>>>>?@<>??>??B=?BBABC>@@?@@?AB>?AA?==??@@???@@@@?ACAAB@BBA?>?D>>@>;?HXG954465635*,HãÔÐȾ¼¾º´¼¼¹¹½¿À½½¿ÁÂÁÀ¿ÄÆÄÂÅÅÁ¿»ÂÀ»¾ÅÄÂÆÄÃÃÃÃÃþ½»¾Â¿¹·½Ê¼¼®F
Code
-----------------
import cv2
#changing message chars to ordinals
def stego_generator(message):
for char in message:
yield ord(char)
#read image into numpy arrray function with cv2
def get_img(img_loc):
img = cv2.imread(img_loc)
return img
#getting greated common denomenator to change pixels
def gcd(x,y):
while(y):
x, y = y, x % y
return x
#steganography encoder function
def encode_stego(img_loc, msg):
img = get_img(img_loc)
msg_gen = stego_generator(msg)
pattern = gcd(len(img), len(img[0]))
for i in range(len(img)):
for j in range(len(img[0])):
if (i+1 * j+1) % pattern == 0:
try:
img[i-1][j-1][0] = next(msg_gen)
except StopIteration:
img[i-1][j-1][0] = 0
return img
#steganography decoder function
def decode_stego(img_loc):
img = get_img(img_loc)
pattern = gcd(len(img), len(img[0]))
message = ''
for i in range(len(img)):
for j in range(len(img[0])):
if (i-1 * j-1) % pattern == 0:
if img[i-1][j-1][0] != 0:
message = message + chr(img[i-1][j-1][0])
else:
return message
print('For PC')
choice = input('Code or Decode\n')
print('What is the location of the Image?')
file_loc = input('Example format> C:\\Users\\username\\folder\\filename.jpg\n')
if choice.lower() == 'code':
to_code = input('What is your message?\n')
secret_message = to_code
encoded_img = encode_stego(file_loc, secret_message)
file_name = input('What would you like to call the Image File?\n')
cv2.imwrite(file_name, encoded_img)
elif choice.lower() == 'decode':
print(decode_stego(file_loc))
else:
print('Command not available')```