Last active
October 3, 2019 19:25
-
-
Save aathil-Mr-ITGuy/000cec3bbeab1b6a4edf1908b256fe9f to your computer and use it in GitHub Desktop.
Image Steganography using python
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
#we have to import Image from PIL library, as I sadi earlier PIL used to extract the details of image, | |
#specially pixels of image | |
from PIL import Image | |
def genrateData(payload_data): | |
newData = [] | |
for i in payload_data: | |
newData.append(format(ord(i), '08b')) | |
return newData | |
# Pixels are modified according to the | |
# 8-bit binary payload_data and finally returned | |
def modPix(pixel, payload_data): | |
datalist = genrateData(payload_data) | |
lendata = len(datalist) | |
imdata = iter(pixel) | |
for i in range(lendata): | |
# Extracting 3 pixelels at a time | |
pixel = [value for value in imdata.__next__()[:3] + | |
imdata.__next__()[:3] + | |
imdata.__next__()[:3]] | |
# Pixel value should be made | |
# odd for 1 and even for 0 | |
for j in range(0, 8): | |
if (datalist[i][j]=='0') and (pixel[j]% 2 != 0): | |
if (pixel[j]% 2 != 0): | |
pixel[j] -= 1 | |
elif (datalist[i][j] == '1') and (pixel[j] % 2 == 0): | |
pixel[j] -= 1 | |
# Eigh^th pixel of every set tells | |
# whether to stop ot read further. | |
# 0 means keep reading; 1 means the | |
# message is over. | |
if (i == lendata - 1): | |
if (pixel[-1] % 2 == 0): | |
pixel[-1] -= 1 | |
else: | |
if (pixel[-1] % 2 != 0): | |
pixel[-1] -= 1 | |
pixel = tuple(pixel) | |
yield pixel[0:3] | |
yield pixel[3:6] | |
yield pixel[6:9] | |
def encode_enc(newImage, payload_data): | |
w = newImage.size[0] | |
(x, y) = (0, 0) | |
for pixel in modPix(newImage.getdata(), payload_data): | |
# Putting modified pixels in the new image | |
newImage.putpixel((x, y), pixel) | |
if (x == w - 1): | |
x = 0 | |
y += 1 | |
else: | |
x += 1 | |
# Encode data into image | |
def encode(): | |
img = input("Enter the image name(Eg: A.png) ") | |
image = Image.open(img, 'r') | |
payload_data = input("Enter Your Message ") | |
if (len(payload_data) == 0): | |
raise ValueError('Please type your Message') | |
newImage = image.copy() | |
encode_enc(newImage, payload_data) | |
new_img_name = input("Enter the name of new image(with extension): ") | |
newImage.save(new_img_name, str(new_img_name.split(".")[1].upper())) | |
# Decode the payload_data in the image | |
def decode(): | |
img = input("Enter image name to decode (b.png):") | |
image = Image.open(img, 'r') | |
payload_data = '' | |
secretData = iter(image.getdata()) | |
while (True): | |
pixels = [value for value in secretData.__next__()[:3] + | |
secretData.__next__()[:3] + | |
secretData.__next__()[:3]] | |
# string of binary payload_data | |
binstr = '' | |
for i in pixels[:8]: | |
if (i % 2 == 0): | |
binstr += '0' | |
else: | |
binstr += '1' | |
payload_data += chr(int(binstr, 2)) | |
if (pixels[-1] % 2 != 0): | |
return payload_data | |
# Main Function | |
def main(): | |
a = int(input(":: Image Steganography::\n" | |
"Choose \n 1. Encoding \n 2. Decoding\n")) | |
if (a == 1): | |
encode() | |
elif (a == 2): | |
print("Decoded succesfully- " + decode()) | |
else: | |
raise Exception("Please try again") | |
# Driver Code | |
if __name__ == '__main__' : | |
# Calling main function | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment