Advertisement
ahmad_zizo

LSB Algorithm

Dec 2nd, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import os, sys
  2. import Image
  3. import binascii
  4. import optparse
  5.  
  6. #hide
  7. def hide(image, message):
  8.     msg = Image.open(message)
  9.     msg = msg.convert("RGBA")
  10.     data = msg.getdata()
  11.  
  12.     img = Image.open(image)
  13.     img = img.convert("RGB")
  14.     data2 = img.getdata()
  15.     newData = []
  16.     index = 0
  17.  
  18.     for item in data:
  19.         if item[0] == 0:
  20.             if (data2[index][0])%2 == 0:
  21.                 newData.append((data2[index][0] + 1, data2[index][1], data2[index][2]))
  22.             else:
  23.                 newData.append((data2[index][0],data2[index][1], data2[index][2]))
  24.             index = index + 1
  25.         else:
  26.             if (data2[index][0])%2 == 1:
  27.                 newData.append((data2[index][0] - 1,data2[index][1], data2[index][2]))
  28.             else:
  29.                 newData.append((data2[index][0],data2[index][1], data2[index][2]))
  30.             index = index + 1
  31.  
  32.     img.putdata(newData)
  33.     img.save("new_"+image, "PNG")
  34.  
  35.     return "Completed!"
  36.  
  37. #extract
  38. def extract(image):
  39.     img = Image.open(image)
  40.     img = img.convert("RGB")
  41.     data = img.getdata()
  42.     orgData = []
  43.  
  44.     for item in data:
  45.         if item[0]%2 == 0:
  46.             orgData.append((255,255,255))
  47.         else:
  48.             orgData.append((0,0,0))
  49.  
  50.     img.putdata(orgData)
  51.     img.save("msg_"+image, "PNG")
  52.  
  53.     return "Completed!"
  54.  
  55. def Main():
  56.         parser = optparse.OptionParser('usage %prog '+\
  57.         '-H/-E <target file>')
  58.     parser.add_option('-H', dest='hide', type='string', \
  59.         help='target picture path to hide another image')
  60.     parser.add_option('-E', dest='extract', type='string', \
  61.         help='target picture path to extract the secret image')
  62.    
  63.     (options, args) = parser.parse_args()
  64.     if (options.hide != None):
  65.         mesg = raw_input("Enter a message to hide: ")
  66.         print hide(options.hide, mesg)
  67.     elif (options.extract != None):
  68.                 print extract(options.extract)
  69.     else:
  70.         print parser.usage
  71.         exit(0)
  72.  
  73.  
  74. if __name__ == '__main__':
  75.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement