Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed toolbox #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added images/decoded_encoded_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/decoded_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/decoded_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/encoded_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 41 additions & 6 deletions steganography.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
from PIL import Image, ImageFont, ImageDraw
import textwrap

def decode_image(file_location="images/encoded_sample.png"):
def decode_image(file_location="images/encoded_sample.png", decoded_file="images/decoded_image.png"):
"""Decodes the hidden message in an image

file_location: the location of the image file to decode. By default is the provided encoded image in the images folder
"""
encoded_image = Image.open(file_location)
red_channel = encoded_image.split()[0]
red_channel = encoded_image.split()[0] # isolate the red_channel from the original RGB image.


x_size = encoded_image.size[0]
y_size = encoded_image.size[1]

decoded_image = Image.new("RGB", encoded_image.size)
pixels = decoded_image.load()

pass #TODO: Fill in decoding functionality
# iterate though each pixel in the encoded image red_channel
for i in range(0,x_size):
for j in range(0,y_size):
# set the decode_image pixel to be (0, 0, 0) or (255, 255, 255)
if red_channel.getpixel((i,j))%2 == 0: #check if number is even
decoded_image.putpixel((i,j),(0,0,0))
else:
decoded_image.putpixel((i,j),(255,255,255))

decoded_image.save("images/decoded_image.png")
decoded_image.save(decoded_file)

def write_text(text_to_write, image_size):
"""Writes text to an RGB image. Automatically line wraps
Expand All @@ -43,11 +51,38 @@ def encode_image(text_to_encode, template_image="images/samoyed.jpg"):
text_to_encode: the text to encode into the template image
template_image: the image to use for encoding. An image is provided by default.
"""
pass #TODO: Fill out this function
encoded_image = Image.open(template_image)
red_channel = encoded_image.split()[0] # isolate the red_channel from the original RGB image.
x_size = encoded_image.size[0]
y_size = encoded_image.size[1]

# Create a black and white picture
text_to_write = text_to_encode
image_size = encoded_image.size
text_decode = write_text(text_to_write, image_size)

# iterate through the picture to encode it
for i in range(0,x_size):
for j in range(0,y_size):
# set the decode_image pixel to be (0, 0, 0) or (255, 255, 255)
pixel=red_channel.getpixel((i,j))
if text_decode.getpixel((i,j)) == (255,255,255):
if pixel%2 == 0: #if even
r, g, b = encoded_image.getpixel((i,j))
encoded_image.putpixel((i,j), (r+1, g, b))# make the lsb odd
else:
if pixel%2 != 0: #if odd
r, g, b = encoded_image.getpixel((i,j))
encoded_image.putpixel((i,j), (r-1, g, b))# make the lsb odd

new_file = "images/encoded_image.png"
encoded_image.save(new_file) # save the encoded picture
text_decode.save("images/decoded_text.png") # save the text image
decode_image(new_file, "images/decoded_encoded_image.png") #save the decoded picture

if __name__ == '__main__':
print("Decoding the image...")
decode_image()

print("Encoding the image...")
encode_image()
encode_image('This is another test')