- Aryan Ebrahimpour
- 5 min read
Steganography is the practice of concealing a message within another message or a physical object. In this blog post we want to hide a photo inside another photo. So when you share the photo, it looks like a single ordinary photo, but it's actually two photos. One of them is hidden inside of the other, and it can only be revealed using an external tool.
Using this method you can hide your signature in your artwork, or store multiple photos instead of one when you're very low on storage in tiny devices. In a similar approach, Pico-8 game cartridges are contained in an small PNG of the game itself1.
Photos as matrices
To be able to manipulate photos and embedding things inside them, we first need to know how they are represented and stored. One way to look at the photos is to see them as a mathematical matrix, where every cell of the matrix represents a pixel, and the value of it is the color of that pixel/cell.
For example for a matrix like this:
We'll get a photo like this:
As you can see in the matrix and the photo, the closer the value is to
255 the whiter the pixel becomes.
0 means black, and anything
255 describes a shade of gray.
Photos as matrices of bits
We've already presented our photos as a bunch of matrices. Let's push our nerdiness a little bit further and represent value of each cell using binaries, instead of decimals (I swear it will come in handy later). The same photo we've seen before will be represented using this matrix:
Now let's find out what happens to the photo if we change the least significant bit (LSB, aka the rightmost bit). So I'm gonna swap all of the LSBs and see how the new matrix and the photo will look like:
Can your eyes detect any changes in the result photo? I assume no, it surely looks like the previous photo (unless you've got supernatural eyes), because we've only changed a single bit with the least significance. With this basic trick we can embed a photo inside another photo without anyone noticing that something is a little bit off (pun intended).
Embed the other photo
For embedding the other photo, we need to replace every LSB of the main photo with every single bit of the hidden photo, which means the hidden photo has to be smaller than the main photo in size.
But how much smaller shall the hidden photo be? For a visible photo of size there are LSBs, which means we can have a hidden photo of the size inside the visible photo. As an example, for a visible photo of the size we have:
That means we can hide a photo with the size of inside of a host photo with size of .
In colorful photos, every pixel of the photo is represented by three matrices. Red matrix, Green matrix, and the Blue matrix. Instead of 3 different matrices, you might also think of it as a single matrix with a tuple of 3 values, all ranging from 0 to 255.
Our approach to hide the photo still works in this case. You just need to replace all the LSBs for every matrix of every cell.
PNG, JPEG, and compressions
As you might already know, some photo formats (like JPEG) heavily compress the source image using different mathematical approaches (e.g. Haar wavelet). So you might ask what happens to the hidden photo in that case? Well, the hidden photo will most likely be destroyed and you'll see a bunch of noises after trying to reveal your secret. But there are other formats like PNG that preserve your photo as is, and you can try this trick using those formats.
I've implemented this method long ago back when I was a bachelor student. You might want to check it out at github.com/avestura/PhotoFiremark. It's a desktop application, and uses Emgu CV to play with photo bits.