Advertisement
Guest User

Steamgifts Steganography Add.Py

a guest
Jul 24th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. from PIL import Image, ImageFilter
  2.  
  3. def get_num_bits_secret():
  4.     return 1
  5.  
  6. #Read host image
  7. visible = Image.open( 'top.png' )
  8. #Read the hidden image
  9. secret = Image.open( 'bot.png' )
  10.  
  11. num_bits_secret = get_num_bits_secret()
  12.  
  13. def combine(visible, secret):
  14.  
  15.     width1, height1 = visible.size
  16.     width2, height2 = secret.size
  17.  
  18.     if width1 != width2 or height1 != height2:
  19.         print("the two images don't have the same dimensions")
  20.         raise 1
  21.  
  22.     ret = visible.copy()
  23.     # iterate through the pixels
  24.     for i in range(width1):
  25.         for j in range(height1):
  26.  
  27.             # bigpixel is the pixel from the host image and smallpixel is the pixel from the hidden image.
  28.             bigpixel = list( ret.getpixel((i,j)) )
  29.             smallpixel = list( secret.getpixel((i,j)) )
  30.  
  31.             # iterate through the 3 color components (RGB).
  32.             for c in range(3):
  33.                 bigvalue = bigpixel[c]
  34.                 smallvalue = smallpixel[c]
  35.  
  36.                 # "<<" and ">>" are bit shifting operators. They make a number lose binary digits at the right side, or add zeros at the right side.
  37.                 # e.g.: x >> 2, makes x lose two bits from the end (it's like dividing by 2^2 and taking only the quotient)
  38.                 # e.g.: x << 2, adds two 0 bits to the right of x (it's like multiplying by 2^2)
  39.  
  40.                 # here, we just replace the last num_bits_secret bits in bigvalue with zeros.
  41.                 bigvalue = ((bigvalue >> num_bits_secret) << num_bits_secret)
  42.  
  43.                 # we keep only the first num_bits_secret from the left side of smallvalue.
  44.                 smallvalue = (smallvalue >> (8 - num_bits_secret))
  45.  
  46.                 # we add them together to get the combined number
  47.                 newvalue = bigvalue + smallvalue
  48.                 bigpixel[c] = newvalue
  49.  
  50.             # place the modified pixel back into the copy of the host image.
  51.             ret.putpixel((i,j), tuple(bigpixel))
  52.  
  53.     return ret
  54.  
  55. combined = combine(visible, secret)
  56. combined.save('combined.png')
  57. # combined.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement