Add option to either pass a path or an Image instance

This commit is contained in:
Peter Justin 2019-04-08 09:57:50 +02:00
parent 538316c722
commit 93de151025
5 changed files with 27 additions and 8 deletions

View file

@ -26,7 +26,10 @@ __revision__ = "$Date: 2017/01/18 $"
__license__ = "GPLv3"
from PIL import Image
import piexif
from stegano import tools
def hide(input_image_file, img_enc, secret_message = None, secret_file = None, img_format = None):
"""Hide a message (string) in an image.
@ -43,7 +46,7 @@ def hide(input_image_file, img_enc, secret_message = None, secret_file = None, i
except:
text = compress(b64encode(secret_message))
img = Image.open(input_image_file)
img = tools.open_image(input_image_file)
if img_format is None:
img_format = img.format
@ -66,7 +69,8 @@ def reveal(input_image_file):
from base64 import b64decode
from zlib import decompress
img = Image.open(input_image_file)
img = tools.open_image(input_image_file)
try:
if img.format in ['JPEG', 'TIFF']:
if 'exif' in img.info:

View file

@ -42,7 +42,7 @@ def hide(input_image: Union[str, IO[bytes]],
message_length = len(message)
assert message_length != 0, "message length is zero"
img = Image.open(input_image)
img = tools.open_image(input_image)
if img.mode not in ['RGB', 'RGBA']:
if not auto_convert_rgb:
@ -96,7 +96,7 @@ def hide(input_image: Union[str, IO[bytes]],
def reveal(input_image: Union[str, IO[bytes]], encoding='UTF-8'):
"""Find a message in an image (with the LSB technique).
"""
img = Image.open(input_image)
img = tools.open_image(input_image)
width, height = img.size
buff, count = 0, 0
bitab = []

View file

@ -45,7 +45,7 @@ def hide(input_image: Union[str, IO[bytes]],
message_length = len(message)
assert message_length != 0, "message length is zero"
img = Image.open(input_image)
img = tools.open_image(input_image)
if img.mode not in ['RGB', 'RGBA']:
if not auto_convert_rgb:
@ -105,7 +105,7 @@ def reveal(input_image: Union[str, IO[bytes]],
encoding: str = 'UTF-8'):
"""Find a message in an image (with the LSB technique).
"""
img = Image.open(input_image)
img = tools.open_image(input_image)
img_list = list(img.getdata())
width, height = img.size
buff, count = 0, 0

View file

@ -29,6 +29,7 @@ import sys
from PIL import Image
from typing import Union, IO
from stegano import tools
def hide(input_image: Union[str, IO[bytes]], message: str):
"""
@ -41,7 +42,7 @@ def hide(input_image: Union[str, IO[bytes]], message: str):
message_length = len(message)
assert message_length != 0, "message message_length is zero"
assert message_length < 255, "message is too long"
img = Image.open(input_image)
img = tools.open_image(input_image)
# Use a copy of image to hide the text in
encoded = img.copy()
width, height = img.size
@ -70,7 +71,7 @@ def reveal(input_image: Union[str, IO[bytes]]):
hidden message characters (ASCII values).
The red value of the first pixel is used for message_length of string.
"""
img = Image.open(input_image)
img = tools.open_image(input_image)
width, height = img.size
message = ""
index = 0

View file

@ -29,6 +29,9 @@ import base64
import itertools
from typing import List, Iterator, Tuple, Union
from functools import reduce
from typing import IO, Iterator, List, Tuple, Union
from PIL import Image
ENCODINGS = {
'UTF-8': 8,
@ -99,3 +102,14 @@ def base642binary(b64_fname: str) -> bytes:
"""
b64_fname += '==='
return base64.b64decode(b64_fname)
def open_image(fname_or_instance: Union[str, IO[bytes]]):
"""Opens a Image and returns it.
:param fname_or_instance: Can either be the location of the image as a
string or the Image.Image instance itself.
"""
if isinstance(fname_or_instance, Image.Image):
return fname_or_instance
return Image.open(fname_or_instance)