Loading
0x00Lesson 1 of 16

Represent an image as data

See that every image is a grid of numbers, and read its size and pixels.

15 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • Describe a grayscale image as a grid of pixel values
  • Read an image’s height, width and individual pixels in Python
  • Estimate how much data an image holds

Computer vision (CV) builds systems that get useful information out of images and video: unlocking a phone with your face, reading a check, spotting a tumor in a scan, keeping a car in its lane, finding “beach” photos in your gallery.

It all starts from one fact: to a computer, an image is just numbers on a grid. Each square of the grid is a pixel. In a grayscale image each pixel holds one brightness value - in the common 8-bit format, from 0 (black) to 255 (white).

image_size.py
1image = [
2    [0, 40, 255],
3    [20, 180, 255],
4]
5print(len(image), len(image[0]))
Output
2 3

A list of rows: len(image) is the height (number of rows) and len(image[0]) the width (pixels per row). image[row][column] reads one pixel.

Images are grids of numbers

Try it

Up to 15 XP

Pixel painter

Pick a brightness and click pixels to paint them. The Python list on the right is the same image - watch it change as you paint.

  • Can you give the face a nose? Which numbers change?
  • Turn off Show pixel values: it is still exactly the same data.
Brush:
Click pixels to paint them
image = [
    [0, 0, 255, 255, 255, 255, 0, 0],
    [0, 255, 0, 0, 0, 0, 255, 0],
    [255, 0, 192, 0, 0, 192, 0, 255],
    [255, 0, 0, 0, 0, 0, 0, 255],
    [255, 0, 128, 0, 0, 128, 0, 255],
    [255, 0, 0, 128, 128, 0, 0, 255],
    [0, 255, 0, 0, 0, 0, 255, 0],
    [0, 0, 255, 255, 255, 255, 0, 0],
]

How big is an image?

A full-HD photo is 1920 pixels wide and 1080 tall: over two million pixels. A color image stores 3 values per pixel (red, green, blue), so that one frame holds more than six million numbers - and a video plays 30 of those every second. That is why vision models need so much computation, and why images are often shrunk before processing.

Key takeaways

  • An image is a grid of pixels; a grayscale pixel is one number from 0 (black) to 255 (white).

  • Height = number of rows, width = pixels per row; read a pixel with image[row][column].

  • Images hold a lot of numbers: height × width × channels.

Lesson quiz

7 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: apply computer vision with Python

Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.

Exercise 1

Measure a tiny image

+25 XP

Read a JSON 2D list of grayscale pixel values. Print its height and width as height width. The image has at least one row.

  • Two by three
  • Three by one
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Exercise 2

Find the brightest pixel

+25 XP

Read a JSON grayscale image. Print the brightest value and where it is, as value row column. If several pixels tie, report the first one, scanning row by row from the top-left.

  • One bright spot
  • A tie
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…