Class 34 — Wednesday, April 13
Chrestomatics and patterning
I need a haiku — I can't think of one I like — Will you please help me
Look both ways
Agenda
- Problem solving
- Image manipulation
Downloads
- Module manip.py
- Module blue.py
- Module bw.py
- Module upleft.py
- Module mirror.py
Image Dimension Reminders
Drawing comparisons to Pointillism
Image manipulation with nested loops: gradient.py
- This program imports
Image
from pillow, and allrandom
module functions.
- Next, we define a 510 by 255 canvas.
- Then, for every position closer to the right-hand side of the image, we set the pixel color to get progressively darker and darker until we get to black.
- How we would change the code to have the color get progressively darker and darker from top to bottom, with the bottom edge of the image being black.
- Brainstorm how we could create a gradient that transitions from one color to some color other than black.
Some possible image transformations
| | Duplicate |
| | Bluing |
| | Two tone |
| | Upper left |
| | Mirror |
Our photo manipulation strategy
- Create a new image by making a copy that tweaks the original image
- Produce the new image by going left to right from top to bottom, starting with the top-left corner (0,0)
- Determining a pixel for a new image will be a local decision (ie, not dependent on color values of nextdoor neighbor pixels)
- Note: not all image manipulations can be carried out this way
Basic photo-manipulation problem-solving pattern
# get dimensions of the original
ow, oh = ...
# set dimensions of the new image
nw, nh = ...
# get a new appropriately sized image
new_image = Image.new( 'RGB', ( nw, nh ) )
# fill in every pixel of the new image
# consider every x value for the new image between x=0 and x=width-1
for nx in range( 0, nw ) :
# in tandem with every y value for the image between y=0 and y=height-1
for ny in range( 0, nh ) :
# select the new spot to be filled in the new image
nspot = (nx ,ny )
# determine the corresponding old spot of interest in the original
ospot = ...
# get the pixel value -- that is, the color-- that is, the rgb value-- at the ospot
opixel = ...
# determine the pixel value-- that is, the rgb color value-- for the new image
npixel = ...
# set the nspot in the new image
...
# return the filled in new image
return new_image
Basic photo-manipulation problem-solving Python function
def manip( original,
size
,color
,at
) :''' Provide a pattern for image manipulation
function
size():
determines new image size based on the originalfunction
color():
determines new pixel's color based on the original pixel's colorfunction
at():
determines new pixel location relative to original'''
# set dimensions of the new image
nw, nh =
size( original )
# get a new appropriately sized image
new_image = Image.new( 'RGB', ( nw, nh ) )
# fill in every pixel of the new image
for nx in range( 0, nw ) : # consider every x value for the new image
for ny in range( 0, nh ) : # in tandem with every y value for the image
# set the spot to be filled in the new image
nspot = ( nx, ny )
# determine the corresponding spot of interest in the original
ospot =
at( nspot, nw, nh )
# get the pixel at the ospot
opixel = original.getpixel( ospot )
# determine the pixel for the new image
npixel =
color( opixel )
# set the nspot in the new image
new_image.putpixel( nspot, npixel )
# return the filled in new image
return new_image
Actual photo-manipulation problem-solving Python function
def
same_size
( image ) :return image.size
def
same_color
( pixel ) :return pixel
def
same_at
( spot, w, h ) :return spot
def manip( original, size=
same_size
, color=same_color
, at=same_at
) :...
More image transformations
| | Flip |
| | Sepia |
| | Grayscale |
| | Clockwise rotation |
| | Palette reduction |
| | Shrinking |
| | Pixelate |
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |