def decode(stego_image, key_file): """ Decodes the stego image based on valid key_file. """ f = open(key_file, 'rb') tup = pickle.load(f) f.close() angles, block_size, key, rev_angles = tup # divide the stego image into same block size again new_width, new_height, width, height, new_inv_dir = split_blocks( stego_image, block_size) # change to the invisible directory. cwd = os.getcwd() os.chdir(new_inv_dir) # find all the images. l = os.listdir(os.getcwd()) # arrange them in increasing order. k = [] counter = 1 while counter != len(l) + 1: k.append(str(counter) + '.png') counter += 1 for block in range(len(k)): # open the image = k[block] I = Image.open(k[block]).convert('RGBA') # rotate the image by reverse angle generated above. I = I.rotate(angles[block]) I.save(k[block]) merge_images.merge_folder(os.getcwd(), block_size, new_width, new_height) # apply lsb_matching expose method. print('Decoding initiated...') key = key.split(' ')[1] msg = lsb_matching.expose_message('final.final.png', int(key)) # remove .final folder. Of no use. tempfolder = os.getcwd() os.chdir(cwd) shutil.rmtree(tempfolder) return msg
def step1(image, block_size=5, dont_delete=False): # call the split_blocks function to split image into blocks. print("Splitting image") new_width, new_height, width, height, invisible_dir = split_blocks(image, block_size) # change to the invisible directory. cwd = os.getcwd() os.chdir(invisible_dir) # find all the images. l = os.listdir(os.getcwd()) # arrange them in increasing order. k = [] counter = 1 while counter != len(l)+1: k.append(str(counter)+'.png') counter += 1 # now k contains all the images in increasing order. # radomly rotate them. angles = [] print("Rotating blocks") for i in k: angles.append(random_angle_rotations(i)) # now merge the rotated images. print("Joining rotated blocks") merge_images.merge_folder(os.getcwd(), block_size, new_width, new_height) # find the main directory. mainDir = os.path.dirname(image) # get image name only, without extension image_name = os.path.basename(image).split('.')[0] # move the generated image to mainDir. tempfolder = os.getcwd() os.rename(os.path.join(tempfolder, 'final.'+image_name+'.png'), os.path.join(mainDir, 'final.'+image_name+'.png')) # temporary folder is of no use now. Delete it! os.chdir(cwd) if not dont_delete: shutil.rmtree(tempfolder) return angles
def pixelate(image, message, block_size=5, dont_delete=False): # call the split_blocks function to split image into blocks. print("Splitting image") new_width, new_height, width, height, invisible_dir = split_blocks( image, block_size) # change to the invisible directory. cwd = os.getcwd() os.chdir(invisible_dir) # find all the images. l = os.listdir(os.getcwd()) # arrange them in increasing order. k = [] counter = 1 while counter != len(l) + 1: k.append(str(counter) + '.png') counter += 1 # now k contains all the images in increasing order. # radomly rotate them. angles = [] print("Rotating blocks") for i in k: angles.append(random_angle_rotations(i)) # now merge the rotated images. print("Joining rotated blocks") merge_images.merge_folder(os.getcwd(), block_size, new_width, new_height) # find the main directory. mainDir = os.path.dirname(image) # get image name only, without extension image_name = os.path.basename(image).split('.')[0] # move the generated image to mainDir. tempfolder = os.getcwd() os.rename(os.path.join(tempfolder, 'final.' + image_name + '.png'), os.path.join(mainDir, 'final.' + image_name + '.png')) # temporary folder is of no use now. Delete it! os.chdir(cwd) if not dont_delete: shutil.rmtree(tempfolder) # raster scan the scrumbled image which is stored in the mainDir. os.chdir(mainDir) # open the scrumbled image. raster_scan() internally converts to grayscale. scrumbled_image = Image.open('final.' + image_name + '.png') scrumbled_image_name = 'final.' + image_name + '.png' # start the procedure of LSB Replacement key = lsb_matching.image_embedding(scrumbled_image_name, message, 'final.' + image_name + '-stego.png') print(key) # ----------------------------------------------------------------------------------------------------# # divide the stego image into same block size again new_width, new_height, width, height, new_inv_dir = split_blocks( 'final.' + image_name + '-stego.png', block_size) rev_angles = (-1 * np.array(angles)).tolist() # change to the invisible directory. cwd = os.getcwd() os.chdir(new_inv_dir) # find all the images. l = os.listdir(os.getcwd()) # arrange them in increasing order. k = [] counter = 1 while counter != len(l) + 1: k.append(str(counter) + '.png') counter += 1 for block in range(len(k)): # open the image = k[block] I = Image.open(k[block]).convert('RGBA') # rotate the image by reverse angle generated above. I = I.rotate(rev_angles[block]) I.save(k[block]) merge_images.merge_folder(os.getcwd(), block_size, new_width, new_height) # get image name only, without extension image_name = os.path.basename(image).split('.')[0] # move the generated image to mainDir. tempfolder = os.getcwd() os.rename(os.path.join(tempfolder, 'final.final.png'), os.path.join(mainDir, 'final.final.png')) # temporary folder is of no use now. Delete it! os.chdir(cwd) if not dont_delete: shutil.rmtree(tempfolder) # ----------------------------------------------------------------------------------------------------- # tup = angles, block_size, key, rev_angles # save this tuple in a file. f = open(scrumbled_image_name + '.dat', 'wb') pickle.dump(tup, f) f.close() os.chdir(cwd) os.remove('final.' + image_name + '.png') os.remove('final.' + image_name + '-stego.png') return 1
def step1(image, block_size=5, dont_delete=False): # call the split_blocks function to split image into blocks. print("Splitting image") new_width, new_height, width, height, invisible_dir = split_blocks( image, block_size) # change to the invisible directory. cwd = os.getcwd() os.chdir(invisible_dir) # find all the images. l = os.listdir(os.getcwd()) # arrange them in increasing order. k = [] counter = 1 while counter != len(l) + 1: k.append(str(counter) + '.png') counter += 1 # now k contains all the images in increasing order. # radomly rotate them. angles = [] print("Rotating blocks") for i in k: angles.append(random_angle_rotations(i)) # now merge the rotated images. print("Joining rotated blocks") merge_images.merge_folder(os.getcwd(), block_size, new_width, new_height) # find the main directory. mainDir = os.path.dirname(image) # get image name only, without extension image_name = os.path.basename(image).split('.')[0] # move the generated image to mainDir. tempfolder = os.getcwd() os.rename(os.path.join(tempfolder, 'final.' + image_name + '.png'), os.path.join(mainDir, 'final.' + image_name + '.png')) # temporary folder is of no use now. Delete it! os.chdir(cwd) if not dont_delete: shutil.rmtree(tempfolder) # raster scan the scrumbled image which is stored in the mainDir. os.chdir(mainDir) # open the scrumbled image. raster_scan() internally converts to grayscale. scrumbled_image = Image.open('final.' + image_name + '.png') scrumbled_image = raster_scan(scrumbled_image) # iterate in consecutive_difference to find the pixels variations greater than threshold parameter 't'. t = 15 # group the pixels in the pair of 2. new_image = list(zip(scrumbled_image[::2], scrumbled_image[1::2])) new_image = np.array(new_image) # select those pairs where difference is greater than 't'. EU = new_image[np.abs(new_image[:, 0].astype(int) - new_image[:, 1].astype(int)) >= t] print(EU) return angles