Hide all images in a directory (sort of)By Pranav Sricharan
December 19, 2018
Look at the amazing collection of pictures that I’ve got! Beautiful aren’t they? Oh wait, you don’t seem to appreciate the beauty behind those images.
Patience, I’m not out of my mind. What you see in the above screenshot is actually a collection of different pictures but anyone who’s looking at is actually going to think that you’re insanely obsessive with clocks and a blue color image with the text “Image” written in it in a beautiful font. The original picture is actually hidden safely behind your obsessive collection 😉
How is this possible?
The common image formats that we use like JPEG and PNG pack the image data in a sort of compressed or encoded format. Photo viewing applications decompress or decode the data and display your image. But JPEG and PNG use different algorithms to encode their data. So your image processing application needs to know the type of image it is processing. It is not as simple as reading the extension of the file such as *.jpg or *.png as we could always rename JPEG images with a PNG extension and confuse the application. So images are framed using headers and footers with data being placed between them. These headers are used to identify the type of image. So even if you save a PNG file with a JPG extension, your application will still be able to find out that it’s a PNG file.
JPEG Header: ffd8ffe000104a464946000101
PNG Header: 89504e470d0a1a0a
Why is this knowledge about headers and footers so important?
Simply because your photo viewer reads the data between the first occurrences of header and footer and chooses to ignore any data that is present in the footer.
You probably would’ve guessed it already, if we store the data of another image before the image we wish to hide, then all applications ignore the actual image data that is behind the misleading image that we used. To get back our original image, we need to remove the misleading image data from the image that we just hid.
The following code snippets perform the above-mentioned process in Python
This code is used to find all png files within the folder “enc” and hide them using data from “clock.png”
This code is used to decrypt all JPEG and PNG files in the folder “enc” and save the result in “dec”