Published on 2016.04.01 A library that hides character data in images using Python. You can also run it from the command line. Works with Python2 series.
install
install
pip install steganography
sample-code
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from steganography.steganography import Steganography
#Embed text in image
path = "/tmp/image/a.jpg "
output_path = "/tmp/image/b.jpg "
text = 'The quick brown fox jumps over the lazy dog.'
Steganography.encode(path, output_path, text)
#Load the text hidden in the image
secret_text = Steganography.decode(output_path)
python
#Embed text in image
>>>steganography -e /tmp/image/a.jpg /tmp/image/b.jpg 'The quick brown fox jumps over the lazy dog.'
#Load the text hidden in the image
>>>steganography -d /tmp/image/b.jpg
The quick brown fox jumps over the lazy dog.
1-1. normalize Scan all the pixels in the image and select the pixels that have a remainder of 1 when each RGB value is divided by 8. Change the color of the selected pixel so that the remainder is not 1.
■ Pixels whose color has been changed by normalize processing
Convert text to hexadecimal. Divide the image into 16 pixels and change the value of the pixel color of the corresponding part so that the remainder becomes 1 when each RGB value is divided by 8.
Read the image with the text hidden, divide the image into 16 pixels, and identify the point pixels where the pixel color is 1 when each RGB value is divided by 8. The hexadecimal value is acquired from the point pixel position, converted from the hexadecimal number to a character string, and output.
Images can be easily manipulated by using pillow.
open-image
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from PIL import Image
path = "/tmp/image/a.jpg "
img = Image.open(path)
img = img.convert('RGB')
for y in range(img.size[1]):
for x in range(img.size[0]):
r, g, b = img.getpixel((x, y))
print(r, g, b)
output
>>> python sample.py
(44, 81, 110)
(75, 109, 137)
(85, 114, 144)
(69, 99, 127)
(68, 105, 131)
(52, 94, 116)
(65, 103, 124)
(108, 130, 154)
(149, 150, 181)
(164, 148, 184)
(152, 132, 169)
....
red
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from PIL import Image
path = "/tmp/image/a.jpg "
output_path = "/tmp/image/sample_red.png "
img = Image.open(path)
img = img.convert('RGB')
def matched_pixel(r, g, b):
return r % 8 == g % 8 == b % 8 == 1
for y in range(img.size[1]):
for x in range(img.size[0]):
r, g, b = img.getpixel((x, y))
if matched_pixel(r, g, b):
#Rewrite the specified coordinates to red
img.putpixel((x, y), (255, 0, 0))
img.save(output_path, "PNG", optimize=True)
3-3. str hex converter
str-hex-converter
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
def to_hex(s):
return s.encode("hex")
def to_str(s):
return s.decode("hex")
base = "aiueo kakikukeko"
print(to_hex(base))
# >>> 616975656f206b616b696b756b656b6f
assert base == to_str(to_hex(base))
Pillow is used to read and write images. The source code of the library is available on github. https://github.com/subc/steganography/blob/master/steganography/steganography.py
Recommended Posts