Class 35 — Monday April 26
Chrestomatics and patterning
Itβs simply smashing β For visitors on the Lawn β If they watch their steps
Look both ways
Agenda
- Problem solving
- Image manipulation
Epistle
- Function manip.py
Gallery
- Spring 2021 art exposition
Module cw.py
- Producing a clockwise rotation of an image
- Background
Module mash.py
- Supports the creation of a new image that is a mash up of two existing images —
bg
andfg
. The new image hasbg
as its background. The pixels infg
that are dissimilar to the back screen color are copied on top of the background.
- Testeter smashing.py is available.
Primary function (already written)
combine( bg, fg, sx, sy, back_screen )
- Returns mash up of background image
bg
and foreground imagefg
, where the pixels infg
that are dissimilar toback_screen
are copied on top of the background.
- The mash up uses location
(sx, sy)
as the spot in the background to start the overlay offg
.
def combine(
bg
,fg
,sx
,sy
,back_screen
) :
bw, bh =
bg
.size # size of background imagefw, fh =
fg
.size # size of foreground image
copy =
bg
.copy() # get a copy of background imagenew_image = copy.convert( 'RGB' ) # to be the basis of new image
for fx in range( 0, fw ) : # consider every (fx, fy)
for fy in range( 0, fh ): # possibility of the new image
fspot = ( fx, fy ) # name spot (fx, fy )
fpixel =
fg
.getpixel( fspot ) # get fspot pixel on fg image
nx =
sx
+ fx # determine the shifted nspot on new imageny =
sy
+ fy # to paint fpixelnspot = ( nx, ny )
# check whether fpixel should be painted on new image at nspot
if (
colorable( nspot, new_image, fpixel, back_screen )
) :new_image.putpixel( nspot, fpixel )
return new_image # return the mash up
Helpful constant (already defined)
SIMILARITY_THRESHOLD = 85
- Usage: values
v1
andv2
are considered similar if the difference betweenv1
andv2
is less thanSIMILARITY_THRESHOLD
.
- Note: the difference between values
v1
andv2
is the absolute value ofv1
-
v2
.
Function previously written
distance( p1, p2 )
- Returns the the sum of RGB differences between colors
p1
andp2
. That is, the sum of thep1
andp2
red-level difference, green-level difference, and blue-level difference.
Functions to be written
similar( p1, p2 )
- Returns
True
if the distance between pixelsp1
andp2
is less than similarity threshold; otherwise, returnsFalse
.
- Should make use of function
distance()
.
is_background( p, back_screen )
- Returns
True
ifp
is similar to the background colorback_screen
; otherwise, returnsFalse
.
- Should make use of function
similar()
.
inbounds( spot, img )
- Returns
True
ifspot
lies onimg
; otherwise, returnsFalse
. That is, it returns whether the x-value and y-value forspot
are nonnegative, thespot
x-value is less thanimg
width, and thespot
y-value is less thanimg
height.
colorable( spot, img, p, back_screen )
- Returns
True
if bothspot
is a location onimg
(i.e., inbounds) and colorp
is not similar to the background colorback_screen
; otherwise, returnsFalse
.
- Should make use of functions
inbounds()
andis_background()
.
π¦ © 2022 Jim Cohoon | Resources from previous semesters are available. |