Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 46 ยท Pixel Operations

Brightness, Contrast and Gamma

Learn point operations on images: brightness and contrast, linear stretching, gamma correction, negatives, and how to avoid uint8 overflow and clipping bugs.

The simplest way to change an image is to treat every pixel on its own: take its value, apply a function, write the result. These are called point operations (or intensity transforms). Brightening a dark photo, increasing contrast, correcting how a display renders mid-tones and making a negative are all point operations. They ignore neighbors, so they are fast, easy to reason about and perfectly vectorized with NumPy.

They are also where the most common beginner bug in image code lives: arithmetic on uint8 arrays that silently wraps around. This lesson covers the main transforms and shows how to do them safely. It builds on Images as Arrays.

Point operations as lookup curves

A point operation is a function s = T(r) from an input intensity r to an output s. Because there are only 256 possible values in a uint8 image, you can think of T as a curve, or even precompute it as a lookup table (LUT) of 256 entries and apply it with fancy indexing: out = lut[img].

flowchart LR
    A["Input image<br/>uint8"] --> B["Convert to float<br/>0..1"]
    B --> C["Apply T(r)<br/>per pixel"]
    C --> D["Clip to 0..1"]
    D --> E["Scale, round,<br/>cast to uint8"]

The safe recipe: leave the integer world, transform, clip, and come back.

Brightness and contrast

The most common curve is a straight line:

s = a ยท r + b

  • b shifts all values up or down: brightness.
  • a stretches or squeezes values around zero: contrast (gain). With a > 1 dark and bright areas move apart; with a < 1 the picture flattens toward gray.

After this transform some values leave the valid range, so they must be clipped.

The uint8 overflow trap

NumPy uint8 math wraps around instead of saturating. 250 + 10 becomes 4, which turns a nearly white pixel nearly black. OpenCV's cv2.add saturates, but plain NumPy + does not. Watch the difference:

Python Overflow versus safe brightening

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The wrapped result [30, 120, 220, 14] makes the brightest pixel dark, a bug that looks like random black speckles in real pictures. The two safe options are to widen the dtype (int16 handles sums and differences) or to move to float.

Python Brightness and contrast on a gradient

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the "clipped" column (the 1% for the original is just its two end pixels, exactly 0 and 1). The brighter and the higher-contrast versions push a share of pixels to pure 0 or pure 1, and once a pixel is clipped, its original value is gone for good: no later operation can recover the detail. Plan your transforms so that important regions stay inside the range.

Linear (min-max) stretching

A flat-looking photo often uses only part of the available range, say values between 90 and 160. A contrast stretch maps the lowest value to 0 and the highest to 255:

s = (r - r_min) / (r_max - r_min)

Using exact min and max is fragile (one hot pixel ruins it), so practical code uses percentiles, for example the 2nd and 98th, and clips everything outside.

Python Percentile contrast stretch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The hot pixel at 255 compresses the plain stretch: most of the picture stays in a narrow band. The percentile version ignores such outliers and uses the whole range.

Gamma correction

Human eyes and monitors are not linear. Doubling the number in a pixel does not look twice as bright. A gamma curve adjusts mid-tones while keeping black and white fixed:

s = r ^ gamma (with r in 0..1)

  • gamma < 1 brightens the mid-tones (the curve bows above the diagonal).
  • gamma > 1 darkens them.
  • gamma = 1 changes nothing.

Because 0 and 1 map to themselves, gamma never clips, which makes it gentler than adding a constant. It is often used to lift detail in dark regions of an underexposed photo.

Python Gamma correction and a lookup table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The lookup table is the efficient way to apply any curve to large uint8 images: lut[img] does a table lookup for every pixel in one vectorized step, and works for any function you can tabulate.

Negatives and other curves

The negative reverses brightness: s = 255 - r for uint8 (or 1 - r for floats). Unlike -r, this stays in range and never wraps. Negatives help to see detail in bright regions of medical images, and show the idea of using a decreasing curve.

Other useful curves you can build as LUTs include:

  • Log transform s = c ยท log(1 + r): expands dark values, compresses bright ones (used for images with a huge dynamic range, such as Fourier spectra).
  • Thresholding: a step curve, which gets its own lesson: Thresholding.
  • Piecewise linear curves that stretch just the range you care about.
Python Negatives, log curve and arbitrary LUTs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The log transform lifts 30 to 158 and 90 to 207, a big boost for dark values, while 255 stays at 255.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is np.array([240], dtype=np.uint8) + 30?

    uint8 arithmetic wraps modulo 256, so 270 becomes 14 and NumPy raises no error. Convert to a wider integer type or float and clip.
  2. 2. What does a gamma value below 1 do to mid-tones in s = r ** gamma (r in 0..1)?

    For r between 0 and 1, raising to a power less than 1 increases the value (0.25 ** 0.5 = 0.5). The end points 0 and 1 are unchanged.
  3. 3. Why do practical contrast stretches use percentiles instead of the exact minimum and maximum?

    One hot or dead pixel sets the min or max, leaving the useful part of the image in a narrow band. Percentiles ignore such outliers.
  4. 4. How do you apply a precomputed 256-entry lookup table lut to a uint8 image img?

    Using the image as an index array looks up the output value for every pixel in one vectorized step.

Summary

  • Point operations map each pixel independently through a function s = T(r); they can be applied as a 256-entry lookup table.
  • Brightness adds a constant, contrast multiplies by a gain; both can clip, and clipped information is lost.
  • Never do arithmetic directly on uint8 arrays near the limits: widen the dtype or use floats in 0..1, clip, then round and cast back.
  • Percentile-based stretching is robust against outlier pixels.
  • Gamma r ** gamma reshapes mid-tones without clipping; negatives use 255 - r; log curves lift dark values.

Next, look at the distribution of all these values at once: Histograms and Equalization.