Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

r/dailyprogrammer #101 intermediate

By: a guest on Sep 28th, 2012  |  syntax: Python  |  size: 2.22 KB  |  views: 304  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ###########steg.py##############
  2. #reddit.com/r/dailyprogrammer
  3. #int-101
  4. #puts data in image
  5.  
  6. import sys, Image
  7.  
  8. #verify correct number of arguments entered
  9. if len(sys.argv) != 3:
  10.     print 'Please specify correct arguments'
  11.     print 'Usage: steg.py {image file} {data file}'
  12.     sys.exit()
  13.  
  14. #get file names from arguments
  15. image_fn = sys.argv[1]
  16. data_fn = sys.argv[2]
  17.  
  18. #read in our image file and declare variables
  19. img = Image.open(image_fn)
  20. pixels = img.load()
  21. im_width = img.size[0]
  22. im_height = img.size[1]
  23.  
  24. #read in our data file and declare variables
  25. with open(data_fn, 'r') as f:
  26.     data = f.read()
  27. b_data = ""
  28. for char in data:
  29.     b_data += '%08d'%int(bin(ord(char))[2:])
  30.    
  31. b_data += '11111111' #used to find end when getting data out
  32.    
  33. #add data to image file
  34. for x in range(im_width):
  35.     for y in range(im_height):
  36.         if len(b_data) == 0:
  37.             break
  38.            
  39.         pix = pixels[x,y]
  40.        
  41.         blueBase = bin(pix[2])[2:] #current pixels blue binary value
  42.         newBlue = blueBase[:-2] + b_data[0:2] #create new binary value
  43.        
  44.         b_data = b_data[2:] #remove used bits from b_data
  45.        
  46.         pixels[x,y] = (pix[0],pix[1],int(newBlue,2)) #put new pixel into image file
  47.     else:
  48.         continue
  49.     break
  50.  
  51. #save new image
  52. img.save('steg.png')
  53.  
  54. ###########desteg.py#############
  55. #desteg.py
  56. #reddit.com/r/dailyprogrammer
  57. #int-101
  58. #pulls data out of image
  59.  
  60. import sys, Image
  61.  
  62. #verify correct number of arguments entered
  63. if len(sys.argv) != 2:
  64.     print 'Please specify correct arguments'
  65.     print 'Usage: desteg.py {image file}'
  66.     sys.exit()
  67.  
  68. #get file names from arguments
  69. image_fn = sys.argv[1]
  70.  
  71. #read in our image file and declare variables
  72. img = Image.open(image_fn)
  73. pixels = img.load()
  74. im_width = img.size[0]
  75. im_height = img.size[1]
  76.  
  77. #get bianry data
  78. b_data = ""
  79. for x in range(im_width):
  80.     for y in range(im_height):
  81.         pix = pixels[x,y]
  82.         b_data += bin(pix[2])[-2:]
  83.        
  84. #get actual data
  85. s_data = ""
  86. for x in range(0,len(b_data),8):
  87.     chunk = b_data[x:x+8]
  88.     if chunk == '11111111':
  89.         break #end of steganography data
  90.    
  91.     char = chr(int(chunk,2))
  92.     s_data += char
  93.  
  94. print s_data