Assignment 28 — image manipulation
Due Monday, April 26
Palette reduction module palette.py
- Implement functions
distance()
andclosest()
. Before defining their purposes, first some context.
- Historically, sometimes dvelopers to save computer memory for special purpose devices perform a palette reduction of colors used in image representation.
| |
- Our reduction scheme will use an eight-color palette.
- Black, White, Red, Green, Blue, Yellow, Magenta, and Cyan; that is,
(0,
0,
0)
,(255,
255,
255)
,(255,
0,
0)
,(0,
255,
0)
,(0,
0,
255)
,(255,
255,
0)
,(255, 0, 255)
, and(0, 255, 255)
- In a palette reduction, a pixel
(r,
g,
b)
is replaced by the pixel in in the palette to which it is closest.
- The distance between pixels
(r1,
g1,
b1)
and(r2,
g2,
b2)
equals
abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)
- Where
abs()
is a built-in function for calculating absolute values.
Function distance( p1, p2 )
- Returns the distance between pixels
p1
andp2
; that is the sum of the red differences, green differences, and blue differences, where difference is an absolute value.
Function closest( opixel )
- Make use of the color palette
PALETTE8
PALETTE8 = [ ( 0, 0, 0), (255,255,255), ( 0,255, 0), ( 0, 0,255),
(255, 0, 0), (255,255, 0), (255, 0,255), ( 0,255,255) ]
- Returns the pixel in
PALETTE8
to whichopixel
is closest
- The module has built-in testing using a mandrill image.
- Possible strategy
- First make a legal guess for the result.
- Check each of the possibilites in
PALETTE8
, whenever you find a possibility that is better than the result, update the result.
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |