CS1315: Introduction to Media Computation

Slides:



Advertisements
Similar presentations
CS2984: Introduction to Media Computation Drawing directly on images.
Advertisements

Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
Python: Making colors and Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Oct 2010 Introduction to Computer Science and Media Computation.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
+ Introduction to Programming My first red-eye removal.
Picture Color Manipulation. Using a Loop Our first picture recipe def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
Download JES Tool JES: Jython Environment for Students
Replacing colors using if We don’t have to do one-to-one changes or replacements of color We can use if to decide if we want to make a change.  We could.
Towards Collaborative Dynabooks In Alan Kay’s vision, the computer’s greatest purpose is to facilitate learning through creation and exploration of multimedia.
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
CS1315: Introduction to Media Computation Referencing pixels directly by index number.
Manipulating Pixels by Range and More on Functions.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying images.
CS1315: Introduction to Media Computation Introduction to Programming.
CS1315: Introduction to Media Computation Making sense of functions.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
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.”
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
Using Squeak for media computation Graphics and sound low-level manipulations in Squeak (Smalltalk)
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,
Chapter 5: Advanced Picture Techniques (partial deck)
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
01-IntroToMediaComp1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
Yet Another Version. More Careful Edge Detection def lineDetect(filename): orig = makePicture(filename) makeBw = makePicture(filename) for x in range(0,getWidth(orig)-1):
CS1315: Introduction to Media Computation Using Loops for Pictures.
Chapter 3: Modifying Pictures using Loops. Chapter Learning Objectives.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Changing colors in an area Part 3: Chromakey.
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CS1315: Introduction to Media Computation Introduction to JES.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
CS1315: Introduction to Media Computation Introduction to Programming.
CS1315: Introduction to Media Computation Transforming pictures by index number.
Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
Intro CS and Media Computation1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
CS1315: Introduction to Media Computation Introduction to JES.
CS1315: Introduction to Media Computation Making sense of functions.
1 CS 177 Week 4 Recitation Slides for Loop if statement and range.
CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.
Multimedia Summer Camp
Chapter 4: Modifying Pictures using Loops
Week 2.
CS1315: Introduction to Media Computation
CS1315: Introduction to Media Computation
Download JES Tool JES: Jython Environment for Students
Chapter 2: Introduction to Programming
Chapter 2: Introduction to Programming
Chapter 4: Modifying Pictures using Loops
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Chapter 3: Modifying Pictures using Loops
Agenda – 1/31/18 Questions? Group practice problems
A Media Computation Cookbook
Chapter 15: Topics in Computer Science: Functional Programming
CS 177 Week 3 Recitation Slides
Chapter 3: Modifying Pictures using Loops
CS 101: Introduction to Computing
CS1315: Introduction to Media Computation
Multimedia Summer Camp
CSC1401 Manipulating Pictures 2
Presentation transcript:

CS1315: Introduction to Media Computation Programming picture manipulations

Administrivia Grades will be available via WebWork <Insert Demo of WebWork here> Does everyone have JES installed now?

Demonstrating: Manipulating Colors >>> print color color r=81 g=63 b=51 >>> print newcolor color r=255 g=51 b=51 >>> print distance(color,newcolor) 174.41330224498358 color r=168 g=131 b=105 >>> print makeDarker(color) color r=117 g=91 b=73 >>> newcolor=pickAColor() >>> print getRed(pixel) 168 >>> setRed(pixel,255) 255 >>> color=getColor(pixel) >>> print color color r=255 g=131 b=105 >>> setColor(pixel,color) >>> newColor=makeColor(0,100,0) >>> print newColor color r=0 g=100 b=0 >>> setColor(pixel,newColor) >>> print getColor(pixel)

We can change pixels directly… >>> file="/Users/guzdial/mediasources/barbara.jpg" >>> pict=makePicture(file) >>> show(pict) >>> setColor(getPixel(pict,10,100),yellow) >>> setColor(getPixel(pict,11,100),yellow) >>> setColor(getPixel(pict,12,100),yellow) >>> setColor(getPixel(pict,13,100),yellow) >>> repaint(pict) But that’s really tedious… Manipulating pictures more cleverly is the next topic…

How do you find out what RGB values you have? And where? Use the MediaTools! (especially useful when testing and debugging…) Demonstrate both mediatools in JES and mediatools application (Squeak). The Squeak one isn’t as powerful for pictures, but it’s more powerful than JES’s for sounds and video.

Use a loop! Our first picture recipe def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="/Users/guzdial/mediasources/katie.jpg" >>> picture=makePicture(file) >>> show(picture) >>> decreaseRed(picture) >>> repaint(picture)

How loops are written for is the name of the command An index variable is used to represent the different values in the loop The word in A function that generates a sequence The index variable will be the name for each value in the sequence, each time through the loop A colon (“:”) And, another block

What happens when a loop is executed The index variable is set to the next (or first, at the beginning) item in the sequence The block is executed The variable is often used inside the block Then execution returns to the for statement, where the index variable gets set to the next item in the sequence Repeat until the sequence is exhausted.

getPixels returns a sequence of pixels Each pixel knows its color and its original picture Change the pixel, you change the picture So the loop below assigns the index variable p to each pixel in the picture picture, one at a time. def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Do we need value? def decreaseRed(picture): Not really: Remember that we can swap names for data or functions that are equivalent. def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) def decreaseRed(picture): for p in getPixels(picture): setRed(p, getRed(p) *0.5)

Let’s walk that through slowly… Here we get a picture object in as input and call it picture def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) picture

Now, get the pixels We get all the pixels from the picture, then make p be the name of each one one at a time def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) picture getPixels() Pixel, color r=135 g=116 b=48 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … p

Get the red value from pixel def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) We get the red value of pixel p and name it value picture getPixels() Pixel, color r=135 g=116 b=48 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … … value = 135 p

Now change the pixel def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Set the red value of pixel p to 0.5 (50%) of value picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p

Then move on to the next pixel def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Move on to the next pixel and name it p picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p

Get its red value Get its red value def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Change value to the new red value at the new p picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p

And change this red value def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Change the red value at pixel p to 50% of value picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=66 g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p

And eventually, we do all pixels We go from this… to this!

“Tracing/Stepping/Walking through” the program What we just did is called “stepping” or “walking through” the program You consider each step of the program, in the order that the computer would execute it You consider what would specifically happen there You write down what values each variable (name) has at each point. It’s one of the most important debugging skills you can have. And everyone has to do a lot of debugging, especially at first.

Did that really work? How can we be sure? Sure, the picture looks different, but did we actually decrease the amount of red? By as much as we thought? Let’s check it!

>>> file = pickAFile() >>> print file C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\barbara.jpg >>> pict = makePicture(file) >>> pixel = getPixel(pict,1,1) >>> print pixel Pixel, color=color r=168 g=131 b=105 >>> decreaseRed(pict) >>> newPixel = getPixel(pict,1,1) >>> print newPixel Pixel, color=color r=84 g=131 b=105 >>> print 168 * 0.5 84.0

Checking it in the MediaTools

If you make something you like… writePictureTo(picture,”filename”) Writes the picture out as a JPEG Be sure to end your filename as “.jpg”! If you don’t specify a full path, will be saved in the same directory as JES.