< Back

Lab 29: Files

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab29 
$ cd lab29 


Practice 1

Write a function called average_file(opened_file), which takes as a parameter an opened file. The opened file is a file that contains only numbers, each on their own line. Your function should return a floating point number, which is the average of all of the numbers from the file.

from io import StringIO # DO NOT REMOVE. NEEDED FOR TEST CASES def average_file(opened_file):

Practice 2

Write a function called non_blank_lines(opened_file), which takes an opened file as a parameter. Your function should return a list of lines from the file, which contains only lines that are not blank.

from io import StringIO # DO NOT REMOVE. NEEDED FOR TEST CASES def non_blank_lines(opened_file):


Steganography

Cryptography can be used to make the contents of a message secret, but sometimes you want to hide that a message is even being sent. Steganography is one way to hide messages, and a common technique takes advantage of our inability to perceive small changes in colors. For example the image below consists of two boxes with different colors.

The left box has the RGB values (161, 187, 150). The box on the right has the RGB values (162, 188, 151). The two boxes appear to be the same color because the RGB values are similar. A message can be hidden in an image by making small changes to the RGB values of an existing image.

A common way of modifying an image is to change only the least significant bits of the RGB values. The least significant bit of a number is the right-most bit and represents the value 20, or 1. Therefore, the most a color value will change if the least significant bit is change is 1, which the above image demonstrates as imperceptible. The following whale image, for example, has a message encoded in it.

Details

Create a function called decode_image(ppm_file_name) in a file called steganography.py. The program should return the secret message that is embedded in the specified image file. The message is encoded as 8-bit ASCII in the least significant bit of the image's color data. Assume that the number of color values in the file is a factor of 8. Once you have tested your program on a simple example, you can test it on the above whale image.

Some things to keep in mind:

Example

>>> image_file = open("demo.ppm", "r")
>>> print(image_file.read())
P3
1 8
255
122
101
215
42
2
128
200
13
64
203
155
130
36
172
91
180
88
77
55
22
44
66
33
99
>>> print(decode_image("demo.ppm"))
abc

Hint

  • You are going to use the accumulator pattern here for strings, accumulating the least significant bit of the color values into a string. A number that is odd has a 1 as the least significant bit, a number that is even has a 0 as the least significant bit.

  • You can convert a binary string to a base-10 integer by using the built-in int function.

  • You can convert an integer to a character using the built-in chr function.

 

Challenge

Write a function that can embed messages into images. The function should take three parameters: a message, an input image file name, and an output image file name. It should read in the input image, modify the least significant bits of the color values to embed the message, and write the new image to the specified output file.