Sub topic 2
Raster graphics: an image made of pixels
This chapter assumes you know what a screen pixel is, can open and save common image files such as .jpg and .png, and have read the previous chapter on how computers represent images. It covers the raster model in detail: how the grid is stored, what the numbers mean, how much memory it takes, and the errors people make most often.
The core idea
A raster image is a rectangular grid of pixels. A pixel (short for "picture element") is the smallest unit of the image, and it holds exactly one color. The image has no shapes, edges, or objects in it. It holds only a color value at each grid position. When you see a circle in a raster image, the file does not contain a circle. It contains many pixels that happen to be colored so your eye reads them as a circle.
Raster images are also called bitmap images. Photos from cameras and phones, screenshots, and scanned documents are all raster images.
How the grid is stored
The grid has a width and a height, both measured in pixels. A 1920 × 1080 image has 1,920 columns and 1,080 rows, which is 2,073,600 pixels. Pixels are usually stored row by row, starting at the top left and moving right, then down.
Each pixel is a small group of numbers. In the most common color model, RGB, a pixel has three channels: red, green, and blue. Each channel is usually an 8-bit number from 0 to 255. Mixing the three channels produces the displayed color.
| Pixel (R, G, B) | Resulting color |
|---|---|
| (0, 0, 0) | Black |
| (255, 255, 255) | White |
| (255, 0, 0) | Pure red |
| (42, 111, 219) | A medium blue |
| (128, 128, 128) | Mid gray |
Many formats add a fourth channel called alpha, which records transparency. An alpha of 0 is fully transparent and 255 is fully opaque. A pixel with four 8-bit channels is called RGBA and uses 4 bytes.
Calculating the size
Because every pixel needs storage, the size of an uncompressed raster image follows a simple formula:
size in bytes = width × height × bytes per pixel
The script below applies it to several common cases.
def raw_size(width, height, bytes_per_pixel):
return width * height * bytes_per_pixel
cases = [
("Full HD, RGB", 1920, 1080, 3),
("Full HD, RGBA", 1920, 1080, 4),
("12 MP phone photo, RGB", 4000, 3000, 3),
("Small icon, RGBA", 64, 64, 4),
]
for name, w, h, bpp in cases:
size = raw_size(w, h, bpp)
print(f"{name}: {size} bytes ({size / 1024 / 1024:.2f} MiB)")Running it prints roughly 5.93 MiB for the Full HD RGB image, 7.91 MiB for the RGBA version, 34.33 MiB for the 12-megapixel photo, and 0.02 MiB for the icon. A phone photo is rarely 34 MiB on disk because formats such as JPEG compress it. Compression is covered in a later chapter. The raw figure still matters, because a program must usually decompress the image into memory before it can draw or edit it. A page with many large photos can use far more memory than the file sizes suggest.
Pixel dimensions are the real size
The number of pixels is the actual information in the image. Everything else, such as inches or centimeters, depends on how the image is later displayed or printed. The next chapters cover resolution and PPI/DPI in full. For now, remember one rule: a raster image has a fixed amount of detail, set when the image was created.
That fixed detail leads to the central limit of the raster model. The file cannot hold detail that was never captured. If you need a larger version, software must invent new pixels by estimating them from their neighbors. This process is called resampling or interpolation.
What happens when you enlarge a raster image
Suppose you enlarge a 100 × 100 image to 400 × 400. The new image needs 160,000 pixels, but you started with 10,000. Software has to fill in the other 150,000. The common methods are:
- Nearest neighbor: each new pixel copies the closest original pixel. The result looks blocky, with visible squares. It keeps hard edges, which suits pixel art.
- Bilinear: each new pixel averages the four nearest original pixels. Edges look smooth but soft.
- Bicubic: each new pixel uses a weighted blend of 16 neighbors. It usually looks sharper than bilinear and is a common default in photo editors.
None of these methods adds real detail. They only choose how the guessing is done. Newer tools that use machine learning can generate plausible detail, but the added detail is a guess, not a recovery of the original.
Reducing an image is safer than enlarging it, because you discard information instead of inventing it. Even so, shrinking can cause artifacts such as jagged edges or moiré patterns (wavy interference in fine repeating textures), so editors use filters designed for downscaling.
You can resample in code. This example uses the Pillow library (pip install pillow) to enlarge a small generated image with two different methods and save the results.
from PIL import Image, ImageDraw
# Build a tiny 16x16 image with a diagonal white line on blue
img = Image.new("RGB", (16, 16), (42, 111, 219))
draw = ImageDraw.Draw(img)
draw.line([(0, 0), (15, 15)], fill=(255, 255, 255), width=1)
# Enlarge to 256x256 with two resampling methods
blocky = img.resize((256, 256), resample=Image.NEAREST)
smooth = img.resize((256, 256), resample=Image.BICUBIC)
blocky.save("line_nearest.png")
smooth.save("line_bicubic.png")
print(img.size, "->", blocky.size)Open both output files. The nearest-neighbor version shows clear staircase squares. The bicubic version blurs the staircase into a soft line. Neither one is a clean diagonal line, because that information was never stored.
Editing a raster image
Editing means changing pixel values. When you paint, erase, blur, or adjust brightness, the program rewrites the numbers at specific positions in the grid. This has useful consequences:
- You can make any change at the pixel level, which is why raster suits photo retouching, texture painting, and fine shading.
- Edits are usually destructive. Once you paint over a region, the old pixels are gone unless you kept a copy or used layers.
- Repeated edits and repeated saves in a lossy format can degrade quality, because each save re-compresses the data.
Layers help with the second point. A layered file (for example, PSD or the layered formats used by other editors) stores several grids that are combined for display, so you can change one without overwriting the others. Each layer is still a raster grid, and the final flattened result is one grid.
Common errors
These mistakes appear again and again when people work with raster images.
- Enlarging a small image for a big use. A 300 × 200 pixel web image stretched across a poster will look blurry or blocky no matter which resampling method you pick. Start from an image with enough pixels for the final size.
- Confusing file size with pixel dimensions. A heavily compressed image can have a small file size and many pixels, or a lossless image can be large on disk with few pixels. Check both numbers.
- Judging sharpness at the wrong zoom level. An image that looks fine fitted to your screen may look soft at 100%, where one image pixel maps to one screen pixel. Check at 100% before you decide.
- Resaving a JPEG repeatedly. Each lossy save discards a little more information. Keep an original or a lossless master and export copies from it.
- Ignoring transparency. A format without an alpha channel, such as JPEG, fills transparent areas with a solid color, usually white or black. Pick a format that supports alpha when you need a see-through background.
- Assuming "high DPI" means high quality. A number stored in a file's metadata does not change the pixel count. A 100 × 100 image tagged "600 DPI" still has only 10,000 pixels.
Strengths of the raster model
The raster model is the right choice when the content is full of continuous tone and fine variation: photographs, painted artwork, textures, and anything captured by a camera or scanner. It handles subtle gradients, shadows, and soft edges naturally, because each pixel can hold any color. Its drawbacks follow from the same structure: file size grows with pixel count, enlarging loses quality, and the image has no editable shapes. A later chapter compares both models side by side and gives decision rules.
Exercise
A designer has a 600 × 400 pixel RGBA image stored uncompressed at 4 bytes per pixel. How many bytes does the pixel data need, and why will enlarging the image to 2400 × 1600 not make it sharper?
Show answer
The pixel data needs 600 × 400 × 4 = 960,000 bytes (about 0.92 MiB). Enlarging to 2400 × 1600 creates 3,840,000 pixels, 16 times as many. The extra 3,360,000 pixels are not captured detail. Software estimates them from neighbors by interpolation, so the result is larger but no sharper, and it often looks soft or blocky.
Exercise
A colleague says, "I set the file to 300 DPI, so this small web image is now print quality." What is wrong with this claim?
Show answer
The DPI value is only metadata that suggests a print size. It does not change the number of pixels. If the image has few pixels, then printing it at a large size spreads those pixels thin and it will look blurry. Print quality depends on having enough pixel dimensions for the intended print size.