| from PIL import Image | |
| def genData(data): | |
| # list of binary codes | |
| # of given data | |
| newd = [] | |
| for i in data: | |
| newd.append(format(ord(i), '08b')) | |
| return newd | |
| # Pixels are modified according to the | |
| # 8-bit binary data and finally returned | |
| def modPix(pix, data): | |
| datalist = genData(data) | |
| lendata = len(datalist) | |
| imdata = iter(pix) | |
| for i in range(lendata): | |
| # Extracting 3 pixels at a time | |
| pix = [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 (pix[j]% 2 != 0): | |
| if (pix[j]% 2 != 0): | |
| pix[j] -= 1 | |
| elif (datalist[i][j] == '1') and (pix[j] % 2 == 0): | |
| pix[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 (pix[-1] % 2 == 0): | |
| pix[-1] -= 1 | |
| else: | |
| if (pix[-1] % 2 != 0): | |
| pix[-1] -= 1 | |
| pix = tuple(pix) | |
| yield pix[0:3] | |
| yield pix[3:6] | |
| yield pix[6:9] | |
| def encode_enc(newimg, data): | |
| w = newimg.size[0] | |
| (x, y) = (0, 0) | |
| for pixel in modPix(newimg.getdata(), data): | |
| # Putting modified pixels in the new image | |
| newimg.putpixel((x, y), pixel) | |
| if (x == w - 1): | |
| x = 0 | |
| y += 1 | |
| else: | |
| x += 1 | |
| # Encode data into image | |
| def encode(): | |
| print "Encode" | |
| img = raw_input("Enter image name(with extension): ") | |
| image = Image.open(img, 'r') | |
| image.show() | |
| data = raw_input("Enter data to be encoded : ") | |
| if (len(data) == 0): | |
| raise ValueError('Data is empty') | |
| newimg = image.copy() | |
| encode_enc(newimg, data) | |
| new_img_name = raw_input("Enter the name of new image(with extension): ") | |
| newimg.save(new_img_name, str(new_img_name.split(".")[1].lower())) | |
| # Decode the data in the image | |
| def decode(): | |
| print "Decode" | |
| img = raw_input("Enter image name(with extension) :") | |
| image = Image.open(img, 'r') | |
| image.show() | |
| data = '' | |
| imgdata = iter(image.getdata()) | |
| while (True): | |
| pixels = [value for value in imgdata.next()[:3] + | |
| imgdata.next()[:3] + | |
| imgdata.next()[:3]] | |
| # string of binary data | |
| binstr = '' | |
| for i in pixels[:8]: | |
| if (i % 2 == 0): | |
| binstr += '0' | |
| else: | |
| binstr += '1' | |
| data += chr(int(binstr, 2)) | |
| if (pixels[-1] % 2 != 0): | |
| return data | |
| # Main Function | |
| def main(): | |
| #declaring key | |
| pkey = 123 | |
| a = int(raw_input(":: Image Steganography ::\n" | |
| "1.Encrypt\n2.Decrypt\n")) | |
| if (a == 1): | |
| print("Encryption") | |
| encode() | |
| print("Data has sucessfully Encrpyted") | |
| elif (a == 2): | |
| print("Decryption") | |
| Pkey = int(raw_input("Enter the key to be decrypted : ")) | |
| if(pkey == Pkey): | |
| print("--Decoded word-- " + decode()) | |
| else : | |
| raise Exception("Enter correct key") | |
| else: | |
| raise Exception("Enter correct input") | |
| # Driver Code | |
| if __name__ == '__main__' : | |
| # Calling main function | |
| main() Support: Delta For more info : https://github.com/SharathHebbar/Image-steganography/blob/master/image.py |
Saturday, March 7, 2020
Image steganography using python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment