Browse Source

proofreading

master
Dimitri Merejkowsky 5 years ago
parent
commit
16ec6dcce8
2 changed files with 16 additions and 8 deletions
  1. +10
    -8
      sources/stegano/stegano.py
  2. +6
    -0
      sources/stegano/test_stegano.py

+ 10
- 8
sources/stegano/stegano.py View File

@@ -1,5 +1,4 @@
import PIL.Image import PIL.Image
import itertools


import sys import sys


@@ -22,9 +21,12 @@ def parse_bitstream(stream):


def by_chunk(iterable, size, fillvalue=0): def by_chunk(iterable, size, fillvalue=0):
"Collect data into fixed-length chunks or blocks" "Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * size
return itertools.zip_longest(*args, fillvalue=fillvalue)

chunk = []
for x in iterable:
chunk.append(x)
if len(chunk) >= size:
yield chunk
chunk = []


def set_bit(old_byte, new_bit): def set_bit(old_byte, new_bit):
b = list(bin(old_byte)) b = list(bin(old_byte))
@@ -33,8 +35,8 @@ def set_bit(old_byte, new_bit):




def main_encrypt(): def main_encrypt():
base_name = sys.argv[2]
message = (sys.argv[3] + "\n").encode()
message = (sys.argv[2] + "\n").encode()
base_name = sys.argv[3]
carrier_name = base_name.replace(".png", ".carrier.png") carrier_name = base_name.replace(".png", ".carrier.png")
base_image = PIL.Image.open(base_name) base_image = PIL.Image.open(base_name)
width, height = base_image.size width, height = base_image.size
@@ -42,7 +44,7 @@ def main_encrypt():
bitstream = get_bitstream(message) bitstream = get_bitstream(message)
for row in range(height): for row in range(height):
for col in range(width): for col in range(width):
r, g, b = base_image.getpixel((col, row))
r, g, b, = base_image.getpixel((col, row))
value = None value = None
try: try:
value = next(bitstream) value = next(bitstream)
@@ -60,7 +62,7 @@ def yield_bits(carrier_image):
width, height = carrier_image.size width, height = carrier_image.size
for row in range(height): for row in range(height):
for col in range(width): for col in range(width):
r, g, b = carrier_image.getpixel((col, row))
r, g, b, = carrier_image.getpixel((col, row))
last_bit = int(bin(r)[-1]) last_bit = int(bin(r)[-1])
yield last_bit yield last_bit




+ 6
- 0
sources/stegano/test_stegano.py View File

@@ -1,6 +1,12 @@
import stegano import stegano




def test_by_chunks():
x = [1, 2, 3, 4, 5, 6]
chunks = stegano.by_chunk(x, 3)
assert list(chunks) == [[1, 2, 3], [4, 5, 6]]


def test_message_conversion(): def test_message_conversion():
message = b"I love you\ngarbage" message = b"I love you\ngarbage"
encoded = list(stegano.get_bitstream(message)) encoded = list(stegano.get_bitstream(message))