
I know, yuck right? Notice a couple of things about this function: We check and see that the product of the height and width coordinates are divisible by the GCD (the pattern variable). Finally, we'll write the ugliest nested loop ever to actually encode the picture.

How to encode a message in a picture generator#
Then we'll make a numpy array of that image, create a generator object for the secret message, and calculate the GCD of the image. We're going to pass it the image location and secret message. Ok, so let's write the actual image encoding function. Since the character represented by unicode 0 can't be imitated on a keyboard, our decoder can assume that a 0 means the message is over. How will we tell the decoder when we're done? Well, you can do whatever you want, but I'm going to set the next pixel to be picked to 0. So if the GCD is 7, we're going to grab every seventh pixel and hide the next part of our secret message in it. We're going to send the length and width of the image to this function and use it to decide which pixels we're going to manipulate. We're going to do this with a function that returns a Python generator object:Īll this is doing is picking the greatest common denominator of x and y. First, we're going to take a secret message and translate it into unicode numbers with the ord() function.

How to encode a message in a picture how to#
But since you have the decoder, you run the image through it and now you know how to make my grandmother's world famous cookies.ĭo you get what this is now? Good! Let's put it into practice. To the world, it looks like I just sent you a cool picture. I could pull up my favorite picture of you and me, encode the recipe into the picture, and post it on your Facebook wall or something. Let's say I want to share a secret recipe with you. Most of the time, we're talking about hiding messages inside image files, but it doesn't have to be image files. Steganography is the practice of hiding secret data inside non-secret data. That's your pixel crash course, we'll come back to this in a bit. Note: This isn't an exact replication, there is sometimes other data in the array, but this is enough for your to understand The numpy array would essentially look like this:Įnter fullscreen mode Exit fullscreen mode To illustrate, imagine we had a tiny tiny 4 pixel picture (2 pixels by 2 pixels) of a 4 tile black and white checkerboard. When we use the cv2 function imread and pass it an image file, that image file is translated into a numpy array containing the RGB value for each pixel in the image. The file tells the computer which pixels to light up and with which color. Image files are basically a serialization of an image's pixels and RGB values. The intensity of each color can be from 0 to 255. The color of light it emits is (normally) based on a combination of the colors red green and blue. Each one of those pixels is capable of emitting a light within a color range. My monitor resolution says "1920x1080" which means my screen is 1920 pixels across and 1080 pixels deep, and 2,073,600 pixels altogether.

Your screen is made up of pixels, and probably quite a few of them. First, you need a quick crash course in pixels. If you don't want to sit through me talking about how this works and what steganography is and kuzza kuzza kuzza then just head over to my Github gist here Fire up your favorite editor and a Python console, this actually won't take too long. I don't mean we're going to make tiny text and hide it in the corner, we're going to dig deeper than that. Today we're going to use the OpenCV library cv2 to encode and decode secret messages inside an image file.
