Same Photo, Different Light

Using Vision ModelsEasy+10 XP+10

Same Photo, Different Light

Colour jitter is one of the four standard augmentations: every time a training photo is used, its lighting is nudged a little, so the model can't depend on one exact exposure. For a grayscale photo, two dials do the work.

  • Contrast ccc scales how far each pixel sits from the photo's average pixel value mmm. With c=2c = 2c=2 every pixel ends up twice as far from the average as before; with c=0.5c = 0.5c=0.5, half as far; with c=0c = 0c=0 the whole photo becomes one flat grey at the average. c=1c = 1c=1 changes nothing.
  • Brightness bbb is then added to every pixel. A positive bbb lightens the photo; a negative one darkens it.

For example, in a photo whose average is 100100100, a pixel at 130130130 sits 303030 above the average. With c=2c = 2c=2 and b=−10b = -10b=−10 it becomes 100+2×30−10=150100 + 2 \times 30 - 10 = 150100+2×30−10=150.

The result must still be a valid 8-bit photo. Round each new value to the nearest whole number. Then any value below 000 becomes 000, and any value above 255255255 becomes 255255255.

Task: write colour_jitter(image, brightness, contrast).

  • image is a list of rows of whole-number pixel values from 0 to 255.
  • mmm is the average of all the pixels in the original photo, worked out once before anything changes.
  • Return a new grid of the same shape holding whole numbers.

No test produces a value exactly halfway between two whole numbers, so any ordinary rounding works.

Examples

image = [[90, 140], [60, 210]]
brightness = 15
contrast = 1.0
returns [[105, 155], [75, 225]]
image = [[90, 140], [60, 210]]
brightness = 0
contrast = 2.0
returns [[55, 155], [0, 255]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run