├── bright.png ├── naturepic.png ├── README.md └── imgstegno.py /bright.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjalkalal/Image-stegnography/HEAD/bright.png -------------------------------------------------------------------------------- /naturepic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pranjalkalal/Image-stegnography/HEAD/naturepic.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-stegnography 2 | using LSB method in python. 3 | 4 | ## Installation 5 | Use the package manager [pip](https://pip.pypa.io/en/stable/) to install 'opencv,pillow and numpy' 6 | ```bash 7 | pip install opencv-python 8 | pip install pillow 9 | pip install numpy 10 | ``` 11 | ## usage 12 | 13 | step-1. clonning--> git clone repo. or download zip file 14 | 15 | step-2. run 'python imgstegno.py' 16 | 17 | step-3. give respective inputes and get outputs 18 | 19 | -------------------------------------------------------------------------------- /imgstegno.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from PIL import Image 4 | 5 | #it convert data in binary formate 6 | 7 | 8 | def data2binary(data): 9 | if type(data) == str: 10 | p = ''.join([format(ord(i), '08b')for i in data]) 11 | elif type(data) == bytes or type(data) == np.ndarray: 12 | p = [format(i, '08b')for i in data] 13 | return p 14 | 15 | 16 | # hide data in given img 17 | 18 | def hidedata(img, data): 19 | data += "$$" #'$$'--> secrete key 20 | d_index = 0 21 | b_data = data2binary(data) 22 | len_data = len(b_data) 23 | 24 | #iterate pixels from image and update pixel values 25 | 26 | for value in img: 27 | for pix in value: 28 | r, g, b = data2binary(pix) 29 | if d_index < len_data: 30 | pix[0] = int(r[:-1] + b_data[d_index]) 31 | d_index += 1 32 | if d_index < len_data: 33 | pix[1] = int(g[:-1] + b_data[d_index]) 34 | d_index += 1 35 | if d_index < len_data: 36 | pix[2] = int(b[:-1] + b_data[d_index]) 37 | d_index += 1 38 | if d_index >= len_data: 39 | break 40 | return img 41 | 42 | 43 | def encode(): 44 | img_name = input("\nenter image name:") 45 | image = cv2.imread(img_name) 46 | img = Image.open(img_name, 'r') 47 | w, h = img.size 48 | data = input("\nenter message:") 49 | if len(data) == 0: 50 | raise ValueError("Empty data") 51 | enc_img = input("\nenter encoded image name:") 52 | enc_data = hidedata(image, data) 53 | cv2.imwrite(enc_img, enc_data) 54 | img1 = Image.open(enc_img, 'r') 55 | img1 = img1.resize((w, h),Image.ANTIALIAS) 56 | # optimize with 65% quality 57 | if w != h: 58 | img1.save(enc_img, optimize=True, quality=65) 59 | else: 60 | img1.save(enc_img) 61 | 62 | # decoding 63 | 64 | def find_data(img): 65 | bin_data = "" 66 | for value in img: 67 | for pix in value: 68 | r, g, b = data2binary(pix) 69 | bin_data += r[-1] 70 | bin_data += g[-1] 71 | bin_data += b[-1] 72 | 73 | all_bytes = [bin_data[i: i + 8] for i in range(0, len(bin_data), 8)] 74 | 75 | readable_data = "" 76 | for x in all_bytes: 77 | readable_data += chr(int(x, 2)) 78 | if readable_data[-2:] == "$$": 79 | break 80 | return readable_data[:-2] 81 | 82 | 83 | def decode(): 84 | img_name = input("\nEnter Image name : ") 85 | image = cv2.imread(img_name) 86 | img=Image.open(img_name,'r') 87 | msg = find_data(image) 88 | return msg 89 | 90 | 91 | def stegnography(): 92 | x = 1 93 | while x != 0: 94 | print('''\nImage stegnography 95 | 1.encode 96 | 2.decode''') 97 | u_in = int(input("\n enter your choice:")) 98 | if u_in == 1: 99 | encode() 100 | else: 101 | ans = decode() 102 | print("\n your message:"+ans) 103 | x = int(input("\nenter 1 for continue otherwise 0:")) 104 | 105 | 106 | stegnography() 107 | --------------------------------------------------------------------------------