Created
August 29, 2019 23:16
-
-
Save erik-whiting/d9988122b5e94b1db4c15236b6d86975 to your computer and use it in GitHub Desktop.
Simple steganography script using cv2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
# Make a generator object for the message to be hidden | |
def char_generator(message): | |
for c in message: | |
yield ord(c) | |
# Convenience function for getting image | |
def get_image(image_location): | |
img = cv2.imread(image_location) | |
return img | |
# We'll use this as the pattern for picking pixels to write to | |
def gcd(x, y): | |
while(y): | |
x, y = y, x % y | |
return x | |
def encode_image(image_location, msg): | |
img = get_image(image_location) | |
msg_gen = char_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 | |
def decode_image(img_loc): | |
img = get_image(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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage in Python console: