3.3. Representing Images

Time Estimate: 90 minutes

3.3.1. Introduction and Goals

In this lesson we continue the exploration of bits and binary numbers that we began in Unit 2. In this case we learn how to use bits, 1s and 0s, to represent images.

The image representation technique demonstrated in the video below is known as run-length encoding (RLE) and it is an image compression technique. Image compression is a type of data compression which can reduce the size (number of bits) of transmitted or stored data.

Learning Objectives: I will learn to
  • convert images to bits using run length encoding and text to bits using ASCII
  • describe how bits can be used to represent digital data, including images, documents, and sounds
  • understand that digital representations are an example of abstraction
Language Objectives: I will be able to
  • compare and contrast lossless and lossy compression techniques
  • use target vocabulary, such as pixel, ASCII, and run length encoding while describing how images and text are stored in memory, with the support of concept definitions and vocabulary notes from this lesson

3.3.2. Learning Activities

POGIL Activity for the Classroom (30 minutes)

After watching the video above, work in POGIL teams or pairs to try your own hand at representing images using the Image Representation Activity Worksheet.

You can click on the pixel buttons below to draw simple images or do the exercises on paper. 

Rows: Columns:

Try drawing in the following RLE compression and see if you get something you recognize. Remember the first number in each row is the number of white pixels.

  0, 6
  4, 1, 1
  3, 1, 2
  2, 1, 3
  1, 1, 4
  0, 6
Continue with the rest of the exercises in the Image Representation Activity Worksheet.

Run-Length Encoding

The following video presentation explores some of the details of RLE image compression (an example of lossless compression) and illustrates some of the ways that images and other data are represented with binary numbers.

  • Fewer bits does not necessarily mean less information.
  • The amount of size reduction from compression depends on both the amount of redundancy in the original data representation and the compression algorithm applied.
  • The amount of compression can vary depending on how many bits are used to represent each pixel in the image.
  • The amount of compression also depends on the number of different colors used in the actual image. For our black and white spaceship there were only 2 colors, so there were relatively few color changes and therefore lots of long runs. If it were a colored spaceship, there would be many color changes and therefore fewer long runs. So we would get much less compression.
  • A lossless compression algorithm is one in which no data are lost; the original data can be completely recovered. An example of lossless compression is RLE.
  • A lossy compression algorithm is one in which some data are lost; the original data cannot be completely restored. An example of lossy compression is JPEG.
  • Lossy data compression algorithms can usually reduce the number of bits stored or transmitted more than lossless compression algorithms.

Other Activities

Your teacher may ask you to do some of the following activities in POGIL teams or pairs.

  1. American Standard Code for Information Interchange (ASCII) is character code that includes 128 characters. Write your own message in binary that someone else could decode using an ASCII to Binary table. Trade messages in class and decode each others.
  2. In web pages and in App Inventor, colors are represented using hexcode for Red, Green, and Blue values. Here’s a color mixer app that lets you explore the different colors that are used in Web pages. You can also try making a Custom Color in App Inventor by changing the Screen's BackgroundColor property to Custom. How many bits are used to represent the colors in Hex Code? Figure out the Hex code for pure red? pure green? pure blue?
  3. Research another image type (e.g. GIF, PNG, BMP, TIFF, etc.) and compare and contrast the data needed to store information about the images. Include what type of compression is used.
  4. If you have a digital camera or a smartphone or tablet, find out what image representation scheme it uses? How come all images are not the same size?
  5. ASCII is one type of character code, but 128 characters is not enough for today’s computers, which can represent Chinese, Hindi, and scripts from many other languages. Today’s computers use a system called Unicode, which has more than 100,000 different characters and covers more than 100 different scripts (languages). Use this Unicode converter to convert these Chinese characters to their hexadecimal Unicode values and to their corresponding decimal values: 国话. Convert these Greek letters: οι. Convert these Russian letters: Я ю.

3.3.3. Summary

In this lesson, you learned how to:

3.3.4. Still Curious?

How do Snapchat filters work?
If you or someone you know uses the social media app Snapchat, they have probably used one of those cool filters. But, how exactly do those filters work? Watch the video below to learn more about the algorithm and pixel data behind Snapchat filters.

How does JPEG encoding work?
JPEG is an example of a lossy compression algorithm. JPEG, which uses the file extension .jpg or .jpeg, is the most common format used today to represent images.The JPEG algorithm was created by the Joint Photographic Experts Group (JPEG), hence its name.  The fact that JPEG is a lossy technique means that some of the information present in the image is lost during compression and cannot be recovered. Here is a video lecture on the JPEG compression algorithm (Teacher Tube version). The compression algorithm involves some math, but the video describes just enough of the math so that you can see how JPEG works. The video is a summary of an excellent, more detailed presentation by Randell Heyman -- you should really check out the Heyman video if you are interested in more of the mathematical details.

How are audio files digitized?
What about audio files? How are they digitized and converted to bits? Watch the following video for a summary of how audio files are converted from analog to digital format. Analog refers to data with values that change continuously, or smoothly, over time, like sound or music files. Analog data is converted to a digital forms, 0s and 1s in binary, using a sampling technique, which means measuring values of the analog signal at regular intervals (usually in time or space) called samples. The samples are measured to figure out the exact bits required to store each sample. The use of digital data to approximate real-world analog data is a great example of abstraction!

Here's a fun activity if you have a computer or tablet with a microphone. Go to https://academo.org/demos/virtual-oscilloscope/ or a different audio recorder and see the sound waves that your voice can produce. Think about how they would be represented in 0s and 1s.

Different audio file extensions compress the audio data in different ways. WAV files are huge because they just store snapshots of digitized values at different points of time with no compression. MP3 formatting compresses audio files by removing parts of the audio signal which the human ear cannot easily hear. They save on space while preserving good audio quality.

3.3.5. Self-Check


Here is a table of some of the technical terms we've introduced in this lesson. Hover over the terms to review the definitions.

lossless compression
lossy compression
run length encoding

Check Your Understanding

Complete the following self-check exercises.

3.3.6. Sample AP CSP Exam Question

3.3.7. Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.

You have attempted of activities on this page