Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageFilter
- def get_num_bits_secret():
- return 1
- #Read host image
- visible = Image.open( 'top.png' )
- #Read the hidden image
- secret = Image.open( 'bot.png' )
- num_bits_secret = get_num_bits_secret()
- def combine(visible, secret):
- width1, height1 = visible.size
- width2, height2 = secret.size
- if width1 != width2 or height1 != height2:
- print("the two images don't have the same dimensions")
- raise 1
- ret = visible.copy()
- # iterate through the pixels
- for i in range(width1):
- for j in range(height1):
- # bigpixel is the pixel from the host image and smallpixel is the pixel from the hidden image.
- bigpixel = list( ret.getpixel((i,j)) )
- smallpixel = list( secret.getpixel((i,j)) )
- # iterate through the 3 color components (RGB).
- for c in range(3):
- bigvalue = bigpixel[c]
- smallvalue = smallpixel[c]
- # "<<" and ">>" are bit shifting operators. They make a number lose binary digits at the right side, or add zeros at the right side.
- # e.g.: x >> 2, makes x lose two bits from the end (it's like dividing by 2^2 and taking only the quotient)
- # e.g.: x << 2, adds two 0 bits to the right of x (it's like multiplying by 2^2)
- # here, we just replace the last num_bits_secret bits in bigvalue with zeros.
- bigvalue = ((bigvalue >> num_bits_secret) << num_bits_secret)
- # we keep only the first num_bits_secret from the left side of smallvalue.
- smallvalue = (smallvalue >> (8 - num_bits_secret))
- # we add them together to get the combined number
- newvalue = bigvalue + smallvalue
- bigpixel[c] = newvalue
- # place the modified pixel back into the copy of the host image.
- ret.putpixel((i,j), tuple(bigpixel))
- return ret
- combined = combine(visible, secret)
- combined.save('combined.png')
- # combined.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement