crzcas

encoding message

Jan 2nd, 2021 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.22 KB | None | 0 0
  1. #import Image module from PIL library
  2. from PIL import Image
  3.  
  4. #path to image to hide within here
  5. image = Image.open('houses.png')
  6. rgb_image = image.convert('RGB') #convert to RGB
  7.  
  8. width, height = image.size #assign the image width and height to variables
  9.  
  10. message = input("What message do you want to hide?")
  11.  
  12. #I'm going to use the message "hello everybody!" as an example
  13.  
  14. binary_message = ""
  15.  
  16. #The for loop will repeat for each character of "hello everybody!", including the space and the exclamation mark
  17.  
  18. for char in message:
  19.     ascii_number = ord(char) #This converts the ASCII character into the denary number that represents it
  20.                              #For the first letter, 'h', this gives '104'
  21.                              
  22.     bin_as_string = bin(ascii_number) #This converts that denary number into a string representing a binary number
  23.                                       #So '104' is converted into the string '0b1101000'
  24.                                       #The 'b' is added in by 'bin()' to show the number is binary
  25.                                      
  26.     bin_as_string = bin_as_string.replace('b','') #This removes the 'b'
  27.                                                   #So finally you get '01101000'
  28.    
  29.     while len(bin_as_string) < 8 :
  30.         bin_as_string = '0' + bin_as_string #This lengthens the binary number string until it is 8 bits
  31.                                             #Our example is already 8 bits long, so it remains '01101000'
  32.  
  33.     binary_message = binary_message + bin_as_string #This result is then added to the binary message
  34.    
  35.     #******************
  36.    
  37.     #Create a grid for the output image
  38. output_image = Image.new('RGB', (width,height))
  39.  
  40. #Create a variable to keep track of how far through the binary message the code has got
  41. i = 0
  42.  
  43. #Set up loops to modify each pixel within the image
  44. for row in range(height):
  45.     for col in range(width):
  46.         r, g, b = rgb_image.getpixel((col, row))
  47.  
  48.         #Start by converting the red value of the pixel to binary  
  49.         bin_r = bin(r)    
  50.        
  51.         #If the binary message has not been completely encoded, the code does the following things
  52.         if i < len(binary_message):    
  53.              
  54.            
  55.             #Replace the final bit with a 1 or a 0 from the message    
  56.             if binary_message[i] == '1':
  57.                 bin_r = bin_r[:-1] + '1'
  58.             else:
  59.                 bin_r = bin_r[:-1] + '0'
  60.                
  61.         #Once the message has been completely encoded, the code replaces every red value's last bit with a zero
  62.         #This isn't good steganography, but it's simple to implement
  63.         else:
  64.             bin_r = bin_r[:-1] + '0'
  65.            
  66.         #The binary number for the red value is converted back to an integer
  67.         new_r = int(bin_r,2)
  68.        
  69.         #This integer is set as the pixel's red value
  70.         output_image.putpixel((col, row), (new_r,g,b))
  71.        
  72.         #The variable i is increased by 1 to move on to the next binary character of the message
  73.         i = i + 1
  74.  
  75. #Once the code has iterated over all pixels in the image to change their red values, the image is saved
  76. output_image.save("encoded_image.png")
  77.  
  78. output_image.show()
Add Comment
Please, Sign In to add comment