All problems
Posterize
Problem
Quantize a grayscale image to a fixed number of evenly spaced levels.
For each pixel, snap to the nearest level with index = round(v * (levels - 1) / 255), then map back with round(index * 255 / (levels - 1)). levels is at least 2 and outputs are integers in [0, 255].
Hint
Map to a level index first, then scale that index back to 0-255.
Test cases
case 1
in: [[[0,100],[200,255]],4]
expected: [[0,85],[170,255]]
case 2
in: [[[0,128,255]],2]
expected: [[0,255,255]]
case 3
in: [[[10,20,30],[240,250,255]],3]
expected: [[0,0,0],[255,255,255]]
Starter codePython
def posterize(image, levels):
# Your code here
pass3 test cases · run your code against them in the editor