Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.image as mpimage
- import matplotlib.pyplot as plt
- import sys
- from PIL import Image
- import stepic
- def decrypt (image):
- image = (image * 255).astype(int)[:,:,0]
- imagearray = image.flatten()
- lenth = ''
- for i in range(16):
- lenth += `imagearray[i] % 2`
- lenth = int(lenth,2)
- message = ''
- for i in range(lenth):
- message += `imagearray[i+16] % 2`
- messagechar = ''
- for i in range(len(message)/8):
- messagechar += chr(int(message[i*8:i*8+8], 2))
- print messagechar
- def encrypt (image, text):
- image = (image * 255).astype(int)[:,:,0]
- imagearray = image.flatten()
- ascii = map(ord, text)
- contents = ''.join(map(lambda x: bin(x)[2:].rjust(8,'0'), ascii))
- bits = len(text)*8
- howmany = bin(bits)[2:].rjust(16,'0')
- sum = howmany+contents
- mask = 0xFE
- for i in range(len(sum)):
- b = int(sum[i])
- imagearray[i] = imagearray[i] & mask | b # x and 0 = 0; 0 or x = x
- mpimage.imsave('bwresult.png', imagearray.reshape(image.shape), vmin=0, vmax=255, cmap='gray')
- def blackwhite(info):
- if (info == 'z'):
- image = mpimage.imread('bw.png')
- text = open('message.txt').read()
- encrypt(image, text)
- else:
- image = mpimage.imread('bwresult.png')
- decrypt(image)
- def color(info):
- if (info == 'z'):
- text = open('message.txt').read()
- image=Image.open('color.png')
- encryptcolor(image, text)
- else:
- image=Image.open('colorresult.png')
- decryptcolor(image)
- def decryptcolor(image):
- d = stepic.decode(image)
- data = d.decode()
- print data
- def encryptcolor(image, text):
- imagecode=stepic.encode(image,text)
- imagecode.save('colorresult.png','PNG')
- u = 'n'
- while (u == 'n'):
- k = raw_input('Chcesz zakodowac w pliku kolorowym, czy czarno-bialym? (k/s)\n')
- if (k == 'k'):
- w = raw_input('\nChcesz zakodowac wiadomosc z pliku message.txt czy odkodowac obraz colorresult.png? (z/o)\n')
- if (w == 'z'):
- color(w)
- else:
- color(w)
- else:
- w = raw_input('\nChcesz zakodowac wiadomosc z pliku message.txt czy odkodowac obraz bwresult.png? (z/o)\n')
- if (w == 'z'):
- blackwhite(w)
- else:
- blackwhite(w)
- u = raw_input('\nCzy chcesz zakonczyc? (t/n)\n')
- if u == 't':
- print('\nBye, bye!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement