How to encrypt grayscale images

9.2: Coding black and white images

Authors:
Publication: 22.11.2016
Learning level: 3
Overview: The students code a black and white picture. You will first compare how a text editing program and an image editing program represent one and the same file. After understanding how coding works, code a small picture yourself and look at the result.
Desired knowledge:
  • Information:
    • An image consists of a grid of image points (pixels).
    • A computer represents every piece of information through one code which uses only two symbols: the digits 0 and 1. This code is called binary code.
    • In black and white images, each pixel can only go through one bit being represented. The bit has the value 0 or 1 (white or black).
Vocabulary: Image point, pixel, resolution
Duration: 1 hour
Material: For each group of two: For the class:
Origin: La main à la pâte, Paris

Educational Notes

We recommend using the XnView image processing program for this lesson [1]. XnView has the following advantages:

  • XnView is free to download.
  • XnView can be used to display,, and files.
  • The program is easy to use.
  • You can easily zoom in or out by clicking on the magnifying glass (with the plus or minus sign).
  • You can update an already open file with the "Ctrl key R". This makes it easier to try out.

In lesson 7.3 we talked about information (binary) coded becomes. In lesson 7.2 we have the term for a very similar activity encrypt used. Encryption and coding describe more or less the same process: You replace characters / information (usually) with numbers. The word encryption is used when, for example, a text is changed in such a way that an unauthorized person cannot read it (it is secret). The coding is not secret: the information is converted into numbers according to a known pattern (key).

initial situation

The teacher comes back to the picture of the apple (worksheet 39, picture A), which was pixelated with 256 pixels (grid 2) in the previous lesson (sending and receiving pictures): "The space researchers want to send the image of the apple to the base station. How can they do that?".

Numerous possibilities are suggested in the following discussion. The teacher writes them all on the board. Some of the ideas are deepened: "There are black and white pixels", "You could transmit every pixel with a flashlight, similar to Morse code. 'Flashlight on' would be a black pixel, 'Flashlight off' a white pixel".

The teacher reminds the students of the binary coding they learned in lesson 7.3 (coding information in binary). "Imagine that the researchers can only send a simple text (an SMS) to describe this picture." Little by little, the students come to the conclusion that a "text" made up of zeros and ones could probably be enough to describe the picture.

Observation: Understand the coding of a black and white image

The students should now check whether a picture can actually be described by a text made up of zeros and ones. The teacher shows the students how to open the bitmap-1.pbm file with a very simple text editor (e.g. Notepad). The open file should look like this:

P1
16 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 1 1 1 1 1 0 1 1 1 1 0 1 0
0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0
0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0
0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The teacher explains that the file is a 16 x 16 pixel digital version of the picture with the apple. She asks the students if they can see the apple in this grid of zeros and ones. Indeed, one can guess the round shape of the apple and the leaf on the right. The character strokes of the picture are represented by the ones. If the students are not convinced, have them replace the zeros with periods in the file (click on the binoculars, then click Replace [or just Ctrl-H], then replace "0" with "."). The apple is now clearly visible (the change can and should be undone with the Ctrl key Z).

The teacher draws the students' attention to the first two lines of the file. There you will find the so-called header data, which describe the format of the file: "P1" means "magic number" and indicates that the image data below the header area are binary coded (that is, consist only of zeros and ones). The indication "16 16" means that the image consists of 16 columns and 16 lines (each one pixel wide and one high). The file therefore contains 16 x 16 = 256 bits (zeros and ones), which represent the 256 pixels of the apple image.

Fig. 1: Pixel image of an apple

The teacher then demonstrates how to open the same bitmap-1.pbm file with an image editing program (for example XnView, see the pedagogical notes above). The apple appears tiny, you have to zoom in on it first. By comparing it with the text file, students can see that all zeros correspond to a white pixel and all ones correspond to a black pixel.

The students repeat the steps that the teacher showed them: They open the bitmap-1.pbm file with both a text editor and an image editing program, as shown in the upper part of worksheet 41 (encoding black and white images). Before the students zoom in on the apple, they look at it with a magnifying glass.

The students summarize once again how the file is structured: P1 indicates that the image data (from line 3) are binary-coded (they only consist of zeros and ones). The second line announces how many columns and rows of pixels the picture has. Then come the actual image data: a grid of zeros (for the white pixels) and ones (for the black pixels).

Scientific Notes

  • The filename extension pbm means portable bitmap. This literally means "(over) portable bit plan".
  • If you enter numbers other than "0" and "1" in the file, these numbers are interpreted modulo 2: 0 results in white, 1 results in black, 2 results in white again, 3 again in black, etc. All odd numbers produce white and all even numbers Numbers black.

Task: Coding a checkerboard pattern

The pupils are given the following task: They are supposed to create a file with the text editor that shows a small checkerboard pattern in the image processing program. The checkerboard pattern has 5 columns and 5 rows and the boxes in the corners are black (see Fig. 2 and worksheet 41). The students open the "empty" file "Bitmap empty", fill it with header and image data and save the file (without closing it). You now open the file with the image processing program and check whether your text file is structured correctly. If not, improve accordingly.

Fig. 2: 5x5 checkerboard pattern

The students must come to the following conclusion:

P1
5 5
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1

Summary

Students summarize what they learned in this lesson.

  • The pixels of an image can be represented by numbers.
  • In a black and white image, each pixel is represented by either a 0 (white pixel) or a 1 (black pixel).
  • To transmit a black and white image, you encode it and transmit the encoded version. Once the coded version has been received, it is decoded to reassemble the image.

The teacher completes the "Information" section of the "What is computer science?" Poster.


footnote

1: Xnview can be downloaded free of charge from the following website: www.xnview.com/de/

Last update: 15.9.2017

© Sonnentaler / La main à la pâte http://www.sonnentaler.net/