Advertisement
Guest User

stega

a guest
May 27th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import matplotlib.image as mpimage
  2. import matplotlib.pyplot as plt
  3. import sys
  4. from PIL import Image
  5. import stepic
  6.  
  7. def decrypt (image):
  8. image = (image * 255).astype(int)[:,:,0]
  9. imagearray = image.flatten()
  10. lenth = ''
  11. for i in range(16):
  12. lenth += `imagearray[i] % 2`
  13. lenth = int(lenth,2)
  14. message = ''
  15. for i in range(lenth):
  16. message += `imagearray[i+16] % 2`
  17. messagechar = ''
  18. for i in range(len(message)/8):
  19. messagechar += chr(int(message[i*8:i*8+8], 2))
  20. print messagechar
  21.  
  22. def encrypt (image, text):
  23. image = (image * 255).astype(int)[:,:,0]
  24. imagearray = image.flatten()
  25. ascii = map(ord, text)
  26. contents = ''.join(map(lambda x: bin(x)[2:].rjust(8,'0'), ascii))
  27. bits = len(text)*8
  28. howmany = bin(bits)[2:].rjust(16,'0')
  29. sum = howmany+contents
  30. mask = 0xFE
  31. for i in range(len(sum)):
  32. b = int(sum[i])
  33. imagearray[i] = imagearray[i] & mask | b # x and 0 = 0; 0 or x = x
  34. mpimage.imsave('bwresult.png', imagearray.reshape(image.shape), vmin=0, vmax=255, cmap='gray')
  35.  
  36. def blackwhite(info):
  37. if (info == 'z'):
  38. image = mpimage.imread('bw.png')
  39. text = open('message.txt').read()
  40. encrypt(image, text)
  41. else:
  42. image = mpimage.imread('bwresult.png')
  43. decrypt(image)
  44.  
  45. def color(info):
  46. if (info == 'z'):
  47. text = open('message.txt').read()
  48. image=Image.open('color.png')
  49. encryptcolor(image, text)
  50. else:
  51. image=Image.open('colorresult.png')
  52. decryptcolor(image)
  53.  
  54. def decryptcolor(image):
  55. d = stepic.decode(image)
  56. data = d.decode()
  57. print data
  58.  
  59. def encryptcolor(image, text):
  60. imagecode=stepic.encode(image,text)
  61. imagecode.save('colorresult.png','PNG')
  62.  
  63. u = 'n'
  64. while (u == 'n'):
  65. k = raw_input('Chcesz zakodowac w pliku kolorowym, czy czarno-bialym? (k/s)\n')
  66. if (k == 'k'):
  67. w = raw_input('\nChcesz zakodowac wiadomosc z pliku message.txt czy odkodowac obraz colorresult.png? (z/o)\n')
  68. if (w == 'z'):
  69. color(w)
  70. else:
  71. color(w)
  72. else:
  73. w = raw_input('\nChcesz zakodowac wiadomosc z pliku message.txt czy odkodowac obraz bwresult.png? (z/o)\n')
  74. if (w == 'z'):
  75. blackwhite(w)
  76. else:
  77. blackwhite(w)
  78. u = raw_input('\nCzy chcesz zakonczyc? (t/n)\n')
  79. if u == 't':
  80. print('\nBye, bye!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement