To find the message hidden in an image, you need a script that does the reverse to the one we’ve just written.
See if you can discover the hidden message encoded in the red values of the following image. I’ve included some code to help you.
The following code iterates over all the pixels in encoded_image.png and extracts the binary value of each red pixel.
#Import Image module from PIL library
from PIL import Image
#Path to image containing message here
image = Image.open('encoded_image.png')
#Convert to RGB
rgb_image = image.convert('RGB')
#Assign the image width and height to variables
width, height = image.size
i = 0
output_bits = ""
output_text = ""
#Set up loops to address each pixel in the image
for row in range(height):
for col in range(width):
r, g, b = rgb_image.getpixel((col, row))
bin_r = bin(r)
output_bits = output_bits + bin_r[-1]
Add code to read the final bit of this binary value and add it to the end of the output_bits string.
The code below takes the string output_bits and converts each byte into an ASCII character until it reaches a byte with a value of 0. Then the code prints out the decoded hidden message.
end = False
while end == False:
byte = int(output_bits[:8],2)
print(byte)
if byte == 0:
end = True
else:
output_text = output_text + chr(byte)
output_bits = output_bits[8:]
print(output_text)
There’s also another message hidden in the image’s green values. Can you modify the code to find this message as well?