Sub topic 2
Raster graphics: an image made of pixels
This chapter assumes you know what a screen pixel is, can open image files on a computer, and recognize extensions such as .jpg and .png. It covers how a raster image stores its data, what that costs in storage, and the errors people most often make with it.
The grid model
A raster image is a rectangular grid of pixels. Each pixel is one small square that holds a single color value. The file records the grid's width and height, then lists the color of every pixel, usually row by row from the top left.
A photo is a good example. A smooth sky is really thousands of neighboring pixels with slightly different blues. Your eye blends them into a continuous tone. That is why raster is the natural model for photographs and scanned artwork. It can record any pattern of color, because each pixel is stored independently.
What one pixel stores
In the common RGB model, a pixel holds three numbers: red, green and blue intensity. With 8 bits per channel, each number runs from 0 to 255. So one pixel takes 3 bytes. Pure red is (255, 0, 0), white is (255, 255, 255), and black is (0, 0, 0).
Some formats add a fourth channel called alpha, which stores transparency. That makes 4 bytes per pixel.
Storage cost
Because every pixel is stored, file size grows with pixel count. The raw size is width × height × bytes per pixel.
width, height = 1920, 1080
bytes_per_pixel = 3 # 8 bits each for red, green and blue
size_bytes = width * height * bytes_per_pixel
print(size_bytes) # 6220800 bytes, about 5.9 MiB uncompressedA single Full HD frame takes almost 6 MiB before compression. Doubling both dimensions quadruples the pixel count, so it quadruples the raw size. Compression, covered in a later chapter, is what makes photos practical to store and send.
Editing a raster image
Editing changes pixel values. A brush stroke recolors the pixels it touches. A blur averages each pixel with its neighbors. Because the file holds no record of "a circle" or "a line," you cannot select a shape afterward and move it as an object. Once you paint over pixels, the old values are gone unless you kept a copy or used layers.
Common errors
These mistakes come up often:
- Enlarging a small image. The file has a fixed number of pixels. Stretching it makes the software invent new pixels by guessing from neighbors, which produces blur or visible blocks. It cannot restore detail that was never captured.
- Judging size by file name or dimensions alone. A 4000 × 3000 image is large in pixels, but how big it looks depends on the display or print size. The next chapters on resolution and scaling cover this.
- Re-saving a lossy file repeatedly. Each save in a lossy format such as JPEG discards a little more data. Edit from an original and export once.
- Expecting sharp edges on diagonal lines. Pixels are square, so diagonal and curved edges show stair-steps called aliasing unless the software smooths them with anti-aliasing, which blends edge pixels.
Raster at a glance
| Property | Raster behavior |
|---|---|
| Stored as | Grid of pixel color values |
| Best for | Photos, scans, painted textures |
| File size depends on | Pixel dimensions and compression |
| Enlarging | Loses sharpness |
| Editing unit | Individual pixels |
Exercise
An uncompressed 3000 × 2000 image uses 3 bytes per pixel. How many bytes does it need, and what happens to that number if you add an alpha channel (4 bytes per pixel)?
Show answer
3000 × 2000 × 3 = 18,000,000 bytes, about 17.2 MiB. With an alpha channel it is 3000 × 2000 × 4 = 24,000,000 bytes, about 22.9 MiB, which is one third larger.
Exercise
A designer enlarges a 300 × 300 pixel web icon to 1200 × 1200 and finds it looks blurry. Explain why in terms of how raster images store data.
Show answer
The file holds only 90,000 pixels. Enlarging to 1200 × 1200 needs 1,440,000 pixels, so the software must invent about 1.35 million new ones by estimating from neighbors. It cannot recover detail that was never stored, so the result looks soft or blocky.