Published a library that hides character data in Python images

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

Data embedding example

Untitled.png

Sample code

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)

Sample command

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. Operation overview-text embedding

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 スクリーンショット 2016-03-31 19.36.12.png

1-2. Text hiding

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.

2. Operation overview-Read text hidden in the image

2-1. Read text

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.

3. Sample implementation code for study

3-1. Code that opens an image and reads it pixel by pixel

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)
....

3-2. Code to change the pixel color of a specific condition to red

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)

sample_red.png

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))

4. Source code

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

Published a library that hides character data in Python images
Code reading of faker, a library that generates test data in Python
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
About psd-tools, a library that can process psd files in Python
Introducing a library that was not included in pip on Python / Windows
Created Simple SQLite, a Python library that simplifies SQLite table creation / data insertion
A memo that I wrote a quicksort in Python
A program that removes duplicate statements in Python
Data analysis in Python: A note about line_profiler
A well-prepared record of data analysis in Python
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
Create a data collection bot in Python using Selenium
Receive dictionary data from a Python program in AppleScript
[Python] How to expand variables in a character string
Try searching for a million character profile in Python
What's in that variable (when running a Python script)
Created a Python library DateTimeRange that handles time ranges
In Python, create a decorator that dynamically accepts arguments Create a decorator
Until you insert data into a spreadsheet in Python
A server that echoes data POSTed with flask / python
[Python] A convenient library that converts kanji to hiragana
Publish / upload a library created in Python to PyPI
A Python program that converts ical data into text
MALSS, a tool that supports machine learning in Python
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
[Python] Leave only the elements that start with a specific character string in the array
A memo that reads data from dashDB with Python & Spark
Take a screenshot in Python
Handle Ambient data in Python
Displaying DICOM images in rudimentary Python as a medical professional
A Python program in "A book that gently teaches difficult programming"
Base64 encoding images in Python 3
A general-purpose program that formats Linux command strings in python
Create a function in Python
Create a dictionary in Python
A function that divides iterable into N pieces in Python
[Python] Creating a GUI tool that automatically processes CSV of temperature rise data in Excel
Display UTM-30LX data in Python
Loop through a generator that returns a date iterator in Python
Let's create a script that registers with Ideone.com in Python.
Developed a library to get Kindle collection list in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Overriding library functions in Python
I tried "a program that removes duplicate statements in Python"
I made a program to collect images in tweets that I liked on twitter with Python
Make a bookmarklet in Python
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
Try building a neural network in Python without using a library
Draw a heart in Python
A Python script that stores 15 years of MLB game data in MySQL in 10 minutes (Baseball Hack!)
A Python program that aggregates time usage from icalendar data
Character code learned in Python
A set of script files that do wordcloud in Python3
A python script that converts Oracle Database data to csv
Play a sound in Python assuming that the keyboard is a piano keyboard
A simple data analysis of Bitcoin provided by CoinMetrics in Python
A memo that handles double-byte double quotes in Python regular expressions
Easy! Implement a Twitter bot that runs on Heroku in Python
[Introduction to Python] How to output a character string in a Print statement