Bryan Ake |
Picture Encoding Program |
Bake3 | |
| Home | |||
|
Program
Here is a python program I wrote when Guzdial was talking about using pictures, sound, etc, and encoding data. This program, linked to below, takes in a .txt file, and the path to a picture. It converts the path of the picture into a picture object, and opens the text file, dumping the contents into a string. Going through the string, it converts each character to it's ASCII value, using the ord() function, adding that value to the red value of the corresponding pixel. If the pixel's value goes over 255, it increments the green value by 1, so that the decoder can detect it and compensate the value. If at any time the loop runs out of characters (via the range in the loop) or runs out of pixels in the picture (via an if statement and a break), the function stops and spits out the picture.
The decoding function works exactly the opposite way - requiring two pictures as input (not the picture paths, due to the way I was testing it, although that would be an easy change in the function) It compares the red values of every pixel, and finds the difference (checking green to see if it needs to compensate for overflow). It converts this ascii value into a character using the chr() function, and adds it to the string. It then spits out a text file containing the message.
These pictures (left is original picture, right is encoded) contain the first large 119,301 characters of Plato's Republic, as retreived from a text file off Project Gutenberg.
| |||