Represent an image as data
See that every image is a grid of numbers, and read its size and pixels.
- 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).
1image = [
2 [0, 40, 255],
3 [20, 180, 255],
4]
5print(len(image), len(image[0]))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 XPPixel 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.
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.
Measure a tiny image
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
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.
Find the brightest pixel
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
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…