Assignment 33 — image manipulation
Due Monday, November 23
Palette reduction module palette.py
- Your task is to implement functions
distance()
andcolor()
. Before defining their purposes, first some context.
- Sometimes to save computer memory, developers for some 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 color( 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.
© 2020 Jim Cohoon | Resources from previous semesters are available. |