Working with ranges in pictures Barb Ericson Georgia
Working with ranges in pictures Barb Ericson Georgia Institute of Technology Feb 2010 02 -Ranges. In. Pictures 1
Learning Goals • How do you create a range of values? • What is a two-dimensional array? • How do you loop through a two-dimensional array? – Nested loops • How do you simplify a hard problem? • How do you copy one picture to another? • How do you make a general function? 02 -Ranges. In. Pictures 2
Try the following in JES >>> print range (0, 3) >>> print range (5, 7) >>> print range (0, 10) >>> print range (3, 1) What do you think the range function does?
Creating ranges of values You can create a range of values in python >>> print range (0, 3) [0, 1, 2] is an array of values >>> print range (0 , 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print range (3, 1) [] [] is an array with no values
Pictures are really two dimensional • Pictures have a width and a height – get. Width(picture) – get. Height(picture) • You can access a pixel of a picture by using the x and y values for the pixel – pixel = get. Pixel(picture, x, y)
Working with part of a picture • What if you want to only modify part of a picture? – Not every pixel in the picture • You need to be able to say where you want to start and stop – Using ranges for x in range(0, get. Width(picture)): for y in range(0, get. Height(picture) / 2): pixel=get. Pixel(picture, x, y)
Challenge • Create a version of decrease red that only changes the red in the top half of the picture
Mirroring a Picture • If you put a vertical mirror in the middle of a picture and looked in the mirror you would see something strange
Mirroring from left to right
What is the Vertical Mirror for this? • Try the solve the problem for small samples • If you can’t solve it on a small sample – You can’t write a program to solve it 0 1 2 3 1 4 5 6 2 7 8 9 0 0 1 2
Mirror Vertical Algorithm • Loop through all the rows (y starts at 0 and is less than the picture height) – Loop with x starting at 0 and x less than the midpoint (mirror point) value • Get the left pixel at x and y • Get the right pixel at width – 1 -x • Set the color for the right pixel to be the color of the left pixel 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 1 2 3 2 1 5 4 3 4 5 1 2 3 2 1
Algorithm to Code def mirror. Vertical(source ): mirror. Point = get. Width(source) / 2 width = get. Width(source) for y in range(0, get. Height(source )): for x in range(0, mirror. Point ): left. Pixel = get. Pixel(source, x, y) right. Pixel = get. Pixel(source, width - x - 1, y) color = get. Color(left. Pixel) set. Color(right. Pixel , color)
Challenge – right to left? • Copy mirror. Vertical and modify it to mirror from right to left instead – What part of the function needs to change? 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 4 5 1 2 3 2 1 4 5 3 4 5
Mirror Horizontal • What about mirroring around a mirror held horizontally in the vertical center of the picture? – Like a reflection in a lake?
Thinking Through Mirror Horizontal • Again think of a number at each x and y location – Instead of a color – And try it with a small sample • How can we write a nested for loop to do this? 0 1 2 3 1 4 5 6 2 7 8 9 0 1 2 3 1 4 5 6 2 1 2 3
What is the horizontal mirror of this? 0 • Try to solve the problem for several small samples problems • See if you can come up with the algorithm to solve it – Test it more small samples 1 2 3 4 5 1 6 7 8 9 10 2 11 12 13 14 15 0
Mirror Horizontal Algorithm • Get the height midpoint – Picture height / 2 • Loop through all the x values – Loop from y=0 to y < vertical midpoint • Get the top pixel – At x and y • Get the bottom pixel – Height - 1 - y • Set the bottom pixel’s color to the top pixel color 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 1 2 3
Challenge • Write the function to mirror top to bottom • Write the function to mirror from bottom to top • How about diagonal mirroring? – In just a square section
Mirror part of a picture • Can we mirror just part of a picture?
Mirror part of the temple def mirror. Temple (): source = make. Picture(get. Media. Path("temple. jpg")) mirror. Point = 277 for x in range (14, mirror. Point ): for y in range (28 , 98): print "Copying color from", x, y, "to", mirror. Point+mirror. Point -1 -x, y pleft = get. Pixel(source, x, y) pright = get. Pixel(source , mirror. Point+mirror. Point -1 -x, y) set. Color(pright , get. Color(pleft )) show(source) return source fixed. Temple = mirror. Temple()
Copying Pixels to a New Picture • Need to track the source picture x and y – And the target picture x and y 1 2 3 4 • We can use a blank picture – As the target picture • Several blank pictures are available – 640 x 480. jpg – 7 in. X 95 in. jpg 1 2 3 4
Copy Picture Algorithm • Copy a picture to the 7 by 9. 5 inch blank picture – Create the target picture object – Invoke the method on the target picture • Create the source picture object • Loop through the source picture pixels – Get the source and target pixels – Set the color of the target pixel to the color of the source pixel
Copying pixels • In general, what we want to do is to keep track of a source. X and source. Y, and a target. X and target. Y. – We increment (add to them) in pairs • source. X and target. X get incremented together • source. Y and target. Y get incremented together – The tricky parts are: • Setting values inside the body of loops • Incrementing at the bottom of loops
Copy picture code def copy. Barb (): # Set up the source and target pictures barbf=get. Media. Path("barbara. jpg") barb = make. Picture(barbf) canvasf = get. Media. Path("7 in. X 95 in. jpg") canvas = make. Picture(canvasf) # Now , do the actual copying target. X = 0 for source. X in range(0, get. Width(barb )): target. Y = 0 for source. Y in range(0, get. Height(barb )): color = get. Color(get. Pixel(barb , source. X , source. Y )) set. Color(get. Pixel(canvas , target. X , target. Y), color) target. Y = target. Y + 1 target. X = target. X + 1 show(barb) show(canvas) return canvas
Challenge • How do we change where we copy the picture to? – Can we start the copy somewhere other than 0, 0?
Create a Collage
Collage Code def create. Collage (): flower 1=make. Picture(get. Media. Path("flower 1. jpg")) flower 2=make. Picture(get. Media. Path("flower 2. jpg")) canvas=make. Picture(get. Media. Path("640 x 480. jpg")) #First picture , at left edge target. X =0 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Second picture , 100 pixels over target. X =100 for source. X in range(0, get. Width(flower 2 )): target. Y=get. Height(canvas)-get. Height(flower 2 )-5 for source. Y in range(0, get. Height(flower 2 )): px=get. Pixel(flower 2 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Third picture , flower 1 negated negative(flower 1) target. X =200 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Fourth picture , flower 2 with no blue clear. Blue(flower 2) target. X =300 for source. X in range(0, get. Width(flower 2 )): target. Y=get. Height(canvas)-get. Height(flower 2 )-5 for source. Y in range(0, get. Height(flower 2 )): px=get. Pixel(flower 2 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage code - continued #Fifth picture , flower 1 , negated with decreased red decrease. Red(flower 1) target. X =400 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source cx=get. Pixel(canvas , target. X , target) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1 show(canvas) return(canvas)
General Copy Function def copy(source , target , targ. X , targ. Y ): target. X = targ. X for source. X in range(0, get. Width(source )): target. Y = targ. Y for source. Y in range(0, get. Height(source )): px=get. Pixel(source , source. X , source. Y) tx=get. Pixel(target , target. X , target. Y) set. Color(tx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Better Collage Function def create. Collage 2 (): flower 1=make. Picture(get. Media. Path("flower 1. jpg")) flower 2=make. Picture(get. Media. Path("flower 2. jpg")) canvas=make. Picture(get. Media. Path("640 x 480. jpg")) h 1 = get. Height(flower 1) h 2 = get. Height(flower 2) hc = get. Height(canvas) #First picture , at left edge copy(flower 1 , canvas , 0, hc - h 1 – 5) #Second picture , 100 pixels over copy(flower 2 , canvas , 100 , hc – h 2 – 5)
Better Collage - continued #Third picture , flower 1 negated negative(flower 1) copy(flower 1 , canvas , 200 , hc – h 1 – 5) #Fourth picture , flower 2 with no blue clear. Blue(flower 2) copy(flower 2 , canvas , 300 , hc – h 2 – 5) #Fifth picture , flower 1 , negated with decreased red decrease. Red(flower 1) copy(flower 1 , canvas , 400 , hc – h 1 – 5) return canvas
Challenge • Create your own collage – Use at least 1 picture – Use at least 3 image filters – Mirror the results
Summary • You can create ranges of values – range(10) = 0 – 9 – range(0, 10) • You can loop through a picture – Using x and y ranges • You can loop through part of a picture – By starting and ending at different values than normal • You can break long methods into shorter parts – Pull out common code and put it in a method
Creating ranges of values You can create a range of values in python >>> print range (0, 3) [0, 1, 2] is an array of values >>> print range (0 , 10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print range (3, 1) [] [] is an array with no values
Pictures are really two dimensional • Pictures have a width and a height – get. Width(picture) – get. Height(picture) • You can access a pixel of a picture by using the x and y values for the pixel – pixel = get. Pixel(picture, x, y)
Working with part of a picture • What if you want to only modify part of a picture? – Not every pixel in the picture • You need to be able to say where you want to start and stop – Using ranges for x in range(0, get. Width(picture)): for y in range(0, get. Height(picture) / 2): pixel=get. Pixel(picture, x, y)
Challenge • Create a version of decrease red that only changes the red in the top half of the picture
Mirroring a Picture • If you put a vertical mirror in the middle of a picture and looked in the mirror you would see something strange
Mirroring from left to right
What is the Vertical Mirror for this? • Try the solve the problem for small samples • If you can’t solve it on a small sample – You can’t write a program to solve it 0 1 2 3 1 4 5 6 2 7 8 9 0 0 1 2
Mirror Vertical Algorithm • Loop through all the rows (y starts at 0 and is less than the picture height) – Loop with x starting at 0 and x less than the midpoint (mirror point) value • Get the left pixel at x and y • Get the right pixel at width – 1 -x • Set the color for the right pixel to be the color of the left pixel 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 1 2 3 2 1 5 4 3 4 5 1 2 3 2 1
Algorithm to Code def mirror. Vertical(source ): mirror. Point = get. Width(source) / 2 width = get. Width(source) for y in range(0, get. Height(source )): for x in range(0, mirror. Point ): left. Pixel = get. Pixel(source, x, y) right. Pixel = get. Pixel(source, width - x - 1, y) color = get. Color(left. Pixel) set. Color(right. Pixel , color)
Challenge – right to left? • Copy mirror. Vertical and modify it to mirror from right to left instead – What part of the function needs to change? 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 4 5 1 2 3 2 1 4 5 3 4 5
Mirror Horizontal • What about mirroring around a mirror held horizontally in the vertical center of the picture? – Like a reflection in a lake?
Thinking Through Mirror Horizontal • Again think of a number at each x and y location 0 1 2 3 – Instead of a color – And try it with a small sample 1 4 5 6 • How can we write a nested for loop to do this? 2 7 8 9 0 1 2 3 1 4 5 6 2 1 2 3
What is the horizontal mirror of this? 0 • Try to solve the problem for several small samples problems • See if you can come up with the algorithm to solve it – Test it more small samples 1 2 3 4 5 1 6 7 8 9 10 2 11 12 13 14 15 0
Mirror Horizontal Algorithm • Get the height midpoint – Picture height / 2 • Loop through all the x values – Loop from y=0 to y < vertical midpoint 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 1 2 3 • Get the top pixel – At x and y • Get the bottom pixel – Height - 1 - y • Set the bottom pixel’s color to the top pixel color
Challenge • Write the function to mirror top to bottom • Write the function to mirror from bottom to top • How about diagonal mirroring? – In just a square section
Mirror part of a picture • Can we mirror just part of a picture?
Mirror part of the temple def mirror. Temple (): source = make. Picture(get. Media. Path("temple. jpg")) mirror. Point = 277 for x in range (14, mirror. Point ): for y in range (28 , 98): print "Copying color from", x, y, "to", mirror. Point+mirror. Point -1 -x, y pleft = get. Pixel(source, x, y) pright = get. Pixel(source , mirror. Point+mirror. Point -1 -x, y) set. Color(pright , get. Color(pleft )) show(source) return source fixed. Temple = mirror. Temple()
Copying Pixels to a New Picture • Need to track the source picture x and y – And the target picture x and y 1 2 3 4 • We can use a blank picture – As the target picture • Several blank pictures are available – 640 x 480. jpg – 7 in. X 95 in. jpg 1 2 3 4
Copy Picture Algorithm • Copy a picture to the 7 by 9. 5 inch blank picture – Create the target picture object – Invoke the method on the target picture • Create the source picture object • Loop through the source picture pixels – Get the source and target pixels – Set the color of the target pixel to the color of the source pixel
Copying pixels • In general, what we want to do is to keep track of a source. X and source. Y, and a target. X and target. Y. – We increment (add to them) in pairs • source. X and target. X get incremented together • source. Y and target. Y get incremented together – The tricky parts are: • Setting values inside the body of loops • Incrementing at the bottom of loops
Copy picture code def copy. Barb (): # Set up the source and target pictures barbf=get. Media. Path("barbara. jpg") barb = make. Picture(barbf) canvasf = get. Media. Path("7 in. X 95 in. jpg") canvas = make. Picture(canvasf) # Now , do the actual copying target. X = 0 for source. X in range(0, get. Width(barb )): target. Y = 0 for source. Y in range(0, get. Height(barb )): color = get. Color(get. Pixel(barb , source. X , source. Y )) set. Color(get. Pixel(canvas , target. X , target. Y), color) target. Y = target. Y + 1 target. X = target. X + 1 show(barb) show(canvas) return canvas
Challenge • How do we change where we copy the picture to? – Can we start the copy somewhere other than 0, 0?
Create a Collage
Collage Code def create. Collage (): flower 1=make. Picture(get. Media. Path("flower 1. jpg")) flower 2=make. Picture(get. Media. Path("flower 2. jpg")) canvas=make. Picture(get. Media. Path("640 x 480. jpg")) #First picture , at left edge target. X =0 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Second picture , 100 pixels over target. X =100 for source. X in range(0, get. Width(flower 2 )): target. Y=get. Height(canvas)-get. Height(flower 2 )-5 for source. Y in range(0, get. Height(flower 2 )): px=get. Pixel(flower 2 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Third picture , flower 1 negated negative(flower 1) target. X =200 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage Code - continued #Fourth picture , flower 2 with no blue clear. Blue(flower 2) target. X =300 for source. X in range(0, get. Width(flower 2 )): target. Y=get. Height(canvas)-get. Height(flower 2 )-5 for source. Y in range(0, get. Height(flower 2 )): px=get. Pixel(flower 2 , source. X , source. Y) cx=get. Pixel(canvas , target. X , target. Y) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Collage code - continued #Fifth picture , flower 1 , negated with decreased red decrease. Red(flower 1) target. X =400 for source. X in range(0, get. Width(flower 1 )): target. Y=get. Height(canvas)-get. Height(flower 1 )-5 for source. Y in range(0, get. Height(flower 1 )): px=get. Pixel(flower 1 , source. X , source cx=get. Pixel(canvas , target. X , target) set. Color(cx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1 show(canvas) return(canvas)
General Copy Function def copy(source , target , targ. X , targ. Y ): target. X = targ. X for source. X in range(0, get. Width(source )): target. Y = targ. Y for source. Y in range(0, get. Height(source )): px=get. Pixel(source , source. X , source. Y) tx=get. Pixel(target , target. X , target. Y) set. Color(tx , get. Color(px)) target. Y=target. Y + 1 target. X=target. X + 1
Better Collage Function def create. Collage 2 (): flower 1=make. Picture(get. Media. Path("flower 1. jpg")) flower 2=make. Picture(get. Media. Path("flower 2. jpg")) canvas=make. Picture(get. Media. Path("640 x 480. jpg")) h 1 = get. Height(flower 1) h 2 = get. Height(flower 2) hc = get. Height(canvas) #First picture , at left edge copy(flower 1 , canvas , 0, hc - h 1 – 5) #Second picture , 100 pixels over copy(flower 2 , canvas , 100 , hc – h 2 – 5)
Better Collage - continued #Third picture , flower 1 negated negative(flower 1) copy(flower 1 , canvas , 200 , hc – h 1 – 5) #Fourth picture , flower 2 with no blue clear. Blue(flower 2) copy(flower 2 , canvas , 300 , hc – h 2 – 5) #Fifth picture , flower 1 , negated with decreased red decrease. Red(flower 1) copy(flower 1 , canvas , 400 , hc – h 1 – 5) return canvas
Challenge • Create your own collage – Use at least 1 picture – Use at least 3 image filters – Mirror the results
Summary • You can create ranges of values – range(10) = 0 – 9 – range(0, 10) • You can loop through a picture – Using x and y ranges • You can loop through part of a picture – By starting and ending at different values than normal • You can break long methods into shorter parts – Pull out common code and put it in a method
- Slides: 69