02-rangesinpictures1 barb ericson georgia institute of technology oct 2010 working with ranges in...

36
02-RangesInPictures 1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Upload: ralph-henry

Post on 01-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

02-RangesInPictures 1

Barb Ericson

Georgia Institute of Technology

Oct 2010

Working with ranges in pictures

Page 2: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

02-RangesInPictures 2

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?

Page 3: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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?

Page 4: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Creating ranges of values

You can create a range of values in python

>>> print range (0,3)

[0, 1, 2]

>>> print range (0 ,10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> print range (3,1)

[]

[0,1,2] is an array of values

[] is an array with no values

Page 5: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Pictures are really two dimensional

• Pictures have a width and a height– getWidth(picture)– getHeight(picture)

• You can access a pixel of a picture by using the x and y values for the pixel– pixel = getPixel(picture, x, y)

Page 6: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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, getWidth(picture)):

for y in range(0, getHeight(picture) / 2):

pixel=getPixel(picture, x, y)

Page 7: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Challenge

• Create a version of decrease red that only changes the red in the top half of the picture

Page 8: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

Page 9: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Mirroring from left to right

Page 10: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

1 2 3

4 5 6

7 8 9

0 1 2

0

1

2

0 1 2

0

1

2

Page 11: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

Page 12: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Algorithm to Code

def mirrorVertical(source ):

mirrorPoint = getWidth(source) / 2

width = getWidth(source)

for y in range(0, getHeight(source )):

for x in range(0, mirrorPoint ):

leftPixel = getPixel(source, x, y)

rightPixel = getPixel(source, width - x - 1,y)

color = getColor(leftPixel)

setColor(rightPixel ,color)

Page 13: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Challenge – right to left?

• Copy mirrorVertical 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

Page 14: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Mirror Horizontal

• What about mirroring around a mirror held horizontally in the vertical center of the picture?– Like a reflection in a lake?

Page 15: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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?

1 2 3

4 5 6

7 8 9

1 2 3

4 5 6

1 2 3

0 1 2

0

1

2

0 1 2

0

1

2

Page 16: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

What is the horizontal mirror of this?

• 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

6 7 8 9 10

11 12 13 14 15

0 1 2 3 4

0

1

2

Page 17: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

Page 18: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

Page 19: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Mirror part of a picture

• Can we mirror just part of a picture?

Page 20: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Mirror part of the temple

def mirrorTemple (): source = makePicture(getMediaPath("temple.jpg")) mirrorPoint = 277 for x in range (14, mirrorPoint ): for y in range (28 ,98): print "Copying color from",x,y,"to",mirrorPoint+mirrorPoint -1-x,y pleft = getPixel(source,x,y) pright = getPixel(source ,mirrorPoint+mirrorPoint -1-x,y) setColor(pright ,getColor(pleft )) show(source) return source

fixedTemple = mirrorTemple()

Page 21: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Copying Pixels to a New Picture

• Need to track the source picture x and y – And the target picture x

and y

• We can use a blank picture – As the target picture

• Several blank pictures are available– 640x480.jpg– 7inX95in.jpg

1 2

3 4

1 2

3 4

Page 22: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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

Page 23: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Copying pixels

• In general, what we want to do is to keep track of a sourceX and sourceY, and a targetX and targetY.– We increment (add to them) in pairs

• sourceX and targetX get incremented together• sourceY and targetY get incremented together

– The tricky parts are:• Setting values inside the body of loops• Incrementing at the bottom of loops

Page 24: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Copy picture codedef copyBarb ():# Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now , do the actual copying targetX = 0 for sourceX in range(0, getWidth(barb )): targetY = 0 for sourceY in range(0, getHeight(barb )): color = getColor(getPixel(barb ,sourceX ,sourceY )) setColor(getPixel(canvas ,targetX ,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas

Page 25: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Challenge

• How do we change where we copy the picture to? – Can we start the copy somewhere other than 0,0?

Page 26: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Create a Collage

Page 27: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Collage Code

def createCollage (): flower1=makePicture(getMediaPath("flower1.jpg")) flower2=makePicture(getMediaPath("flower2.jpg")) canvas=makePicture(getMediaPath("640 x480.jpg"))

#First picture , at left edge targetX =0 for sourceX in range(0, getWidth(flower1 )): targetY=getHeight(canvas)-getHeight(flower1 )-5 for sourceY in range(0, getHeight(flower1 )): px=getPixel(flower1 ,sourceX ,sourceY) cx=getPixel(canvas ,targetX ,targetY) setColor(cx ,getColor(px)) targetY=targetY + 1 targetX=targetX + 1

Page 28: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Collage Code - continued

#Second picture , 100 pixels over

targetX =100

for sourceX in range(0, getWidth(flower2 )):

targetY=getHeight(canvas)-getHeight(flower2 )-5

for sourceY in range(0, getHeight(flower2 )):

px=getPixel(flower2 ,sourceX ,sourceY)

cx=getPixel(canvas ,targetX ,targetY)

setColor(cx ,getColor(px))

targetY=targetY + 1

targetX=targetX + 1

Page 29: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Collage Code - continued

#Third picture , flower1 negatednegative(flower1)targetX =200for sourceX in range(0, getWidth(flower1 )): targetY=getHeight(canvas)-getHeight(flower1 )-5 for sourceY in range(0, getHeight(flower1 )): px=getPixel(flower1 ,sourceX ,sourceY) cx=getPixel(canvas ,targetX ,targetY) setColor(cx ,getColor(px)) targetY=targetY + 1 targetX=targetX + 1

Page 30: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Collage Code - continued

#Fourth picture , flower2 with no blueclearBlue(flower2)targetX =300for sourceX in range(0, getWidth(flower2 )): targetY=getHeight(canvas)-getHeight(flower2 )-5 for sourceY in range(0, getHeight(flower2 )): px=getPixel(flower2 ,sourceX ,sourceY) cx=getPixel(canvas ,targetX ,targetY) setColor(cx ,getColor(px)) targetY=targetY + 1 targetX=targetX + 1

Page 31: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Collage code - continued

#Fifth picture , flower1 , negated with decreased reddecreaseRed(flower1)targetX =400for sourceX in range(0, getWidth(flower1 )): targetY=getHeight(canvas)-getHeight(flower1 )-5 for sourceY in range(0, getHeight(flower1 )): px=getPixel(flower1 ,sourceX ,source cx=getPixel(canvas ,targetX ,target) setColor(cx ,getColor(px)) targetY=targetY + 1 targetX=targetX + 1show(canvas)return(canvas)

Page 32: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

General Copy Function

def copy(source , target , targX , targY ):

targetX = targX

for sourceX in range(0, getWidth(source )):

targetY = targY

for sourceY in range(0, getHeight(source )):

px=getPixel(source ,sourceX ,sourceY)

tx=getPixel(target ,targetX ,targetY)

setColor(tx ,getColor(px))

targetY=targetY + 1

targetX=targetX + 1

Page 33: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Better Collage Function

def createCollage2 (): flower1=makePicture(getMediaPath("flower1.jpg")) flower2=makePicture(getMediaPath("flower2.jpg")) canvas=makePicture(getMediaPath("640 x480.jpg")) h1 = getHeight(flower1) h2 = getHeight(flower2) hc = getHeight(canvas) #First picture , at left edge copy(flower1 ,canvas ,0, hc - h1 – 5)

#Second picture , 100 pixels over copy(flower2 ,canvas ,100 , hc – h2 – 5)

Page 34: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Better Collage - continued

#Third picture , flower1 negated negative(flower1) copy(flower1 ,canvas ,200 , hc – h1 – 5)

#Fourth picture , flower2 with no blue clearBlue(flower2) copy(flower2 ,canvas ,300 , hc – h2 – 5)

#Fifth picture , flower1 , negated with decreased red decreaseRed(flower1) copy(flower1 ,canvas ,400 , hc – h1 – 5) return canvas

Page 35: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

Challenge

• Create your own collage– Use at least 1 picture– Use at least 3 image filters– Mirror the results

Page 36: 02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures

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