Copying and Transforming Pictures. First, finding the min or max… Next homework asks you to write a function to find the darkest and lightest shade of.

Slides:



Advertisements
Similar presentations
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
Advertisements

CS2984: Introduction to Media Computation Drawing directly on images.
Sound, Part 2 Using range to manipulate samples by index number.
1 CS 177 Week 6 Recitation Slides Scaling Drawing on images Vector-based Vs. Bitmap graphical representation.
Some Utility Functions If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
CSE 8A Lecture 8 Reading for next class: None Prepare for In-term exam 2 PSA4: Collage and Picture Flip, DON’T WAIT (it’s longer than the previous PSAs)
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 5: Advanced Picture Techniques.
First Courses Workshop Day 1 Mark Guzdial College of Computing Georgia Institute of Technology
UWCSE BRIDGE Workshop Aug. 31 – Sept. 3, 2009 Hal Perkins Computer Science & Engineering University of Washington
How to use the Java class libraries Brief documentation of how to do this all with Java.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying images.
Warm-up Simplify. 5x x – a + 2b – (a – 2b) Multiply.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Media Computation Workshop Day 1 Mark Guzdial College of Computing Georgia Institute of Technology
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
Copying: How it works Here's the initial setup:. Copying: How it works 2 After incrementing the sourceY and targetY once (whether in the for or via expression):
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
Creating a poster is easier than you think.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Chapter 4: Modifying Pixels in a Range (partial slide deck)
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
CS1315: Introduction to Media Computation How to design and debug a program: Top-down, bottom-up, and debugging. Using background subtraction and chromakey.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 5: Advanced Picture Techniques.
Chapter 5: Advanced Picture Techniques. Chapter Objectives.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
Collage Assignments So you’ve used Daniel Zingaro’s Stereo Sound Processing or Kevin Wayne’s Guitar Heroine. Or maybe you’ve used Joshua Guerin and Debby.
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
Yet Another Version. More Careful Edge Detection def lineDetect(filename): orig = makePicture(filename) makeBw = makePicture(filename) for x in range(0,getWidth(orig)-1):
Ready, SET, go! By Melissa Dalis Professor Susan Rodger Duke University July 2011.
“But it looks right”: Bugs in non-majors media programs Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Chapter 6: Modifying Pixels by Position
Examples from Georgia Tech’s CS 1315: Introduction to Media Computation Class examples and student work.
Copyright © Curt Hill Further Picture Manipulation Considering position.
CSC 112Introduction to Media Computation 1 Rotating Images.
CS1315: Introduction to Media Computation Transforming pictures by index number.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
Media computation as a context for learning computing Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Chapter 5: Picture Techniques with Selection
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Picture Functions ppp =makePicture(pickAFile())
Chapter 8: Making Sounds by Combining Pieces
Workshop for Programming And Systems Management Teachers
Working with ranges in pictures
Algorithm design and Analysis
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Chapter 4: Modifying Pixels in a Range
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 6
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
CS 177 Week 9 Recitation Slides
CS1315: Introduction to Media Computation
Chapter 4: Modifying Pixels in a Range
Multimedia Summer Camp
Presentation transcript:

Copying and Transforming Pictures

First, finding the min or max… Next homework asks you to write a function to find the darkest and lightest shade of grey in a picture Here is a similar example to find the value of the pixel with the largest red component def findLargestRed(pict): largestSoFar = -1 for p in getPixels(pict): r = getRed(p) if (r > largestSoFar): largestSoFar = r return largestSoFar

Moving pixels across pictures We’ve seen using index variables to track the pixel position we’re working with in a picture. We can copy between pictures, if we keep track of: –The source index variables Where we’re getting the pixels from –The target index variables Where we’re putting the pixels at (Not really copying the pixels: Replicating their color.)

What can you do then? What can you do when copying from one picture to another? –Collages: Copy several pictures onto one –Cropping: You don’t have to take the whole picture –Scaling: Make a picture smaller, or larger when copying it

Blank files in mediasources getMediaPath(“7inX95in.jpg”) gives you a JPEG canvas which prints out as 7x9.5 inches –Letter-sized page with 1 inch margins getMediaPath(“640x480.jpg”) gives a JPEG canvas at a common size: 640 pixels across by 480 pixels high

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

Copying Barb to a canvas def 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 = 1 for sourceX in range(1,getWidth(barb)): targetY = 1 for sourceY in range(1,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

What’s this naming something to itself? targetX = targetX + 1 This isn’t really naming something as itself –targetX + 1 is evaluated It will result in the number after targetX –targetX = then sets the value of targetX The result is that targetX gets incremented by 1

Transformation = Small changes in copying Making relatively small changes in this basic copying program can make a variety of transformations. –Change the targetX and targetY, and you copy wherever you want –Cropping: Change the sourceX and sourceY range, and you copy only part of the program. –Rotating: Swap targetX and targetY, and you end up copying sideways –Scaling: Change the increment on sourceX and sourceY, and you either grow or shrink the image.

Copying into the middle of the canvas def copyBarbMidway(): # 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 = 100 for sourceX in range(1,getWidth(barb)): targetY = 100 for sourceY in range(1,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

Copying: How it works Here’s the initial setup:

Copying: How it works 2 After incrementing the sourceY and targetY once (whether in the for or via expression):

Copying: How it works 3 After yet another increment of sourceY and targetY: When we finish that column, we increment sourceX and targetX, and start on the next column.

Copying: How it looks at the end Eventually, we copy every pixel

Making a collage Could we do something to the pictures we copy in? –Sure! Could either apply one of those functions before copying, or do something to the pixels during the copy. Could we copy more than one picture! –Of course! Make a collage!

def createCollage(): flower1=makePicture(getMediaPath("flower1.jpg")) print flower1 flower2=makePicture(getMediaPath("flower2.jpg")) print flower2 canvas=makePicture(getMediaPath("640x480.jpg")) print canvas #First picture, at left edge targetX=1 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Second picture, 100 pixels over targetX=100 for sourceX in range(1,getWidth(flower2)): targetY=getHeight(canvas)-getHeight(flower2)-5 for sourceY in range(1,getHeight(flower2)): px=getPixel(flower2,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Third picture, flower1 negated negative(flower1) targetX=200 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Fourth picture, flower2 with no blue clearBlue(flower2) targetX=300 for sourceX in range(1,getWidth(flower2)): targetY=getHeight(canvas)-getHeight(flower2)-5 for sourceY in range(1,getHeight(flower2)): px=getPixel(flower2,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 #Fifth picture, flower1, negated with decreased red decreaseRed(flower1) targetX=400 for sourceX in range(1,getWidth(flower1)): targetY=getHeight(canvas)-getHeight(flower1)-5 for sourceY in range(1,getHeight(flower1)): px=getPixel(flower1,sourceX,sourceY) cx=getPixel(canvas,targetX,targetY) setColor(cx,getColor(px)) targetY=targetY + 1 targetX=targetX + 1 show(canvas) return(canvas) Exactly from book

Cropping: Just the face def copyBarbsFace(): # 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 = 100 for sourceX in range(45,200): targetY = 100 for sourceY in range(25,200): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas

Scaling Scaling a picture (smaller or larger) has to do with sampling the source picture differently –When we just copy, we sample every pixel –If we want a smaller copy, we skip some pixels We sample fewer pixels –If we want a larger copy, we duplicate some pixels We over-sample some pixels

Scaling the picture down def copyBarbSmaller(): # 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 sourceX = 1 for targetX in range(100,100+(getWidth(barb)/2)): sourceY = 1 for targetY in range(100,100+(getHeight(barb)/2)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) sourceY = sourceY + 2 sourceX = sourceX + 2 show(barb) show(canvas) return canvas

Scaling Up: Growing the picture To grow a picture, we simply duplicate some pixels We do this by incrementing by 0.5, but only use the integer part. >>> print int(1) 1 >>> print int(1.5) 1 >>> print int(2) 2 >>> print int(2.5) 2

Scaling the picture up def copyBarbLarger(): # 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 sourceX = 1 for targetX in range(10,10+(getWidth(barb)*2)): sourceY = 1 for targetY in range(10,10+(getHeight(barb)*2)): color = getColor(getPixel(barb,int(sourceX),int(sourceY))) setColor(getPixel(canvas,targetX,targetY), color) sourceY = sourceY sourceX = sourceX show(barb) show(canvas) return canvas

Scaling up: How it works Same basic setup as copying and rotating:

Scaling up: How it works 2 But as we increment by only 0.5, and we use the int() function, we end up taking every pixel twice. Here, the blank pixel at (1,1) in the source gets copied twice onto the canvas.

Scaling up: How it works 3 Black pixels gets copied once…

Scaling up: How it works 4 And twice…

Scaling up: How it works 5 The next “column” (x) in the source, is the same “column” (x) in the target.

Scaling up: How it ends up We end up in the same place in the source, but twice as much in the target. Notice the degradation: –Curves get “choppy”: Pixelated

Described in the text, but skipping here. Good things to try: Can you come up with general copy, rotate, copy, and scale functions? –Take input pictures and parameters –Return the canvas the correct transformation applied Also think about generalizing the transformations: –Scaling up and down by non-integer amounts –Rotating by something other than 90 degree increments

Blending Pictures Instead of copying from the source to the target, we can combine the source and target to create a new image Simple technique –Average the red, green, and blue from the source and target –Try putting Barb on the beach

def averageBlending(): # Set up the source and target pictures barb = makePicture(getMediaPath("barbara.jpg")) beach = makePicture(getMediaPath("beach.jpg")) sourceX = 1 for targetX in range(50,50+(getWidth(barb))): sourceY = 1 for targetY in range(100,100+(getHeight(barb))): barbPixel = getPixel(barb, sourceX, sourceY) # Get barb pixel redBarb = getRed(barbPixel) greenBarb = getGreen(barbPixel) blueBarb = getBlue(barbPixel) beachPixel = getPixel(beach, targetX, targetY) # Get beach pixel redBeach = getRed(beachPixel) greenBeach = getGreen(beachPixel) blueBeach = getBlue(beachPixel) color = makeColor((redBarb + redBeach)/2, (greenBarb + greenBeach) / 2, (blueBarb + blueBeach) / 2) setColor(beachPixel, color) sourceY = sourceY + 1 sourceX = sourceX + 1 show(barb) show(beach) return beach

Blending through Averaging

Chromakey What the weather person does Pose in front of a blue or green screen Swap all “blue” or “green” for the background

Example Solution def chromakey2(source,bg): for p in getPixels(source): if (getRed(p)+getGreen(p) < getBlue(p)): setColor(p, getColor(getPixel(bg, getX(p),getY(p)))) return source

Another way of saying the same thing def chromakey(source,bg): # source should have something in front of blue, bg is the new background for x in range(1,source.getWidth()): for y in range(1,source.getHeight()): p = getPixel(source,x,y) # My definition of blue: If the redness + greenness < blueness if (getRed(p) + getGreen(p) < getBlue(p)): #Then, grab the color at the same spot from the new background setColor(p,getColor(getPixel(bg,x,y))) return source

Can I do this by masking in Photoshop? Of course! –How do you think Photoshop does it? But you can do it better, differently, faster, and for more kinds of pictures if you know how it works