Skip to content

Instantly share code, notes, and snippets.

@jnovikov
Created October 20, 2016 16:11
Show Gist options
  • Save jnovikov/d4c61396acad92d4194b5e56d06a38b2 to your computer and use it in GitHub Desktop.
Save jnovikov/d4c61396acad92d4194b5e56d06a38b2 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw # Подключим необходимые библиотеки.
def add_zeroes(num):
n = bin(num)[2:]
c = 8 - len(n)
return c * '0' + n
def create_encrypted(num, bit):
b = []
c = add_zeroes(num)
for i in c:
b.append(i)
b[7] = bit
return int(''.join(b), 2)
plain_text_bytes = ''
f = open('plain.jpg', 'rb')
c = f.read()
f.close()
for i in c:
plain_text_bytes += add_zeroes(i)
max_len = len(plain_text_bytes)
print(plain_text_bytes)
image = Image.open("image.jpg") # Открываем изображение.
draw = ImageDraw.Draw(image) # Создаем инструмент для рисования.
width = image.size[0] # Определяем ширину.
height = image.size[1] # Определяем высоту.
pix = image.load() # Выгружаем значения пикселей.
current = 0
for i in range(height):
for j in range(width):
pixel = pix[j, i]
a = pixel[0]
b = pixel[1]
c = pixel[2]
if current < max_len:
a = create_encrypted(a, plain_text_bytes[current])
current += 1
if current < max_len:
b = create_encrypted(b, plain_text_bytes[current])
current += 1
if current < max_len:
c = create_encrypted(c, plain_text_bytes[current])
current += 1
draw.point((j, i), (a, b, c))
image.save("encrypted.jpg", "JPEG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment