Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select.

Slides:



Advertisements
Similar presentations
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Advertisements

Kayla Dean & Patrick Baker. Original Picture Sunset def sunset(filename): filename = makePicture(pickAFile()) explore(filename) for p in getPixels(filename):
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.
CS 1 with Robots Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Tickertape def tickertape(directory,string): for each frame to generate create an new canvas write the string on canvas slightly left of the previous frame.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 3: Modifying Pictures using Loops.
Graphics in Python using the JES environment
Computer Science 101 Introduction to Programming with Pictures.
+ Introduction to Programming My first red-eye removal.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
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
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
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.
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 Making sense of functions.
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.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
ManipulatingPictures-Mod6-part61 Manipulating Pictures, Arrays, and Loops: Eliminating color, Inversion, grey scale and adjusting for luminance Barb Ericson.
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
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.
Creating a picture in JES Use in the command window of JES (black background at bottom) to set the folder where the picture is to be saved >>> path = setMediaPath()
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
ITEC 109 Multimedia Lecture Lecture 23. Photo manipulation Review Lists / Arrays.
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.
CS 101: Introduction to Computing Programs that change Pictures Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
Intro CS and Media Computation1 Barb Ericson Georgia Institute of Technology Feb 2010 Introduction to Computer Science and Media Computation.
Python programming Using the JES picture functions and defining new functions.
CS1315: Introduction to Media Computation Making sense of functions.
1 CS 177 Week 4 Recitation Slides for Loop if statement and range.
Python/JES JES IDE (Integrated Development Environment)
Chapter 4: Modifying Pictures using Loops
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Multimedia Summer Camp
Week 2.
Download JES Tool JES: Jython Environment for Students
Barb Ericson Georgia Institute of Technology August 2005
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
Practice with loops! What is the output of each function below?
A Media Computation Cookbook
CSE 113 A February , 2009.
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Georgia Institute of Technology
February , 2009 CSE 113 B.
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
CS 177 Week 3 Recitation Slides
Chapter 3: Modifying Pictures using Loops
CS1315: Introduction to Media Computation
Multimedia Summer Camp
CS1315: Introduction to Media Computation
Presentation transcript:

Python: Working with pixels

Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select candidate.”)

Relational Operators a > b a < b a == b (equality) a <= b (“less than or equal”) a >= b (“greater than or equal”) != (inequality) Note: The order of signs matters: =! = do not work! There should be no spaces inside the relational operator, so “a < = b” also does not work (See also Appendix A.4, p362)

Getting the leading zero into the filename: if n < 10: filename = “swatch0” + str(n) + “.jpg” else: filename = “swatch” + str(n) + “.jpg”

Getting the leading zero into the filename: if n < 10: filename = “swatch0” + str(n) + “.jpg” else: # here we know n >= 10 filename = “swatch” + str(n) + “.jpg” What if you need more frames? if n < 10: filename = “swatch00” + str(n) + “.jpg” else: # here we know n >= 10 if n < 100: # here n is in the range of 10 … 99 filename = “swatch0” + str(n) + “.jpg” else: # here we know n >= 100 filename = “swatch” + str(n) + “.jpg”

Examples from lab 11 Be sure to review these! Open examples.py in JESexamples.py Load In the command area, try the functions Here are links to small images you can use to test your program (or use your own): eye.jpg, luca.jpgeye.jpgluca.jpg

Getting at the pixels getPixel(picture,x,y) Gets a single pixel – returns pixel at position x,y of picture getPixels(picture) gets all the pixels – returns an array containing all the pixels of picture

Reminder: Manipulating Pictures >>> pic1 = makeEmptyPicture(200,100) >>> seafoam = makeColor(153, 255, 204) >>> setAllPixelsToAColor(pic1, seafoam) >>> addText(pic1,30,50,“hello") >>> show(pic1) >>> pic2 = makePicture(pickAFile()) >>> show(pic2)

Color:(108,86,142) Position: (12,9) x = 12 y = 9 red=108green=86blue=142 >>> p = getPixel(picture,12,9) >>> print p Pixel, red=108 green=86 blue=142

What can we do with a pixel p? getRed(p), getGreen(p) getBlue(p) setRed(p, value) setGreen(p, value) setBlue(p, value) functions that take a pixel (p) as input and return a value between 0 and 255 functions that set the value of pixel (p) to a given value between 0 and 255

We can also get, set, and modify Colors getColor(p) takes a pixel as input and returns a Color object with the color at that pixel setColor(p, c) sets the color of pixel (p) as input and a color (c), then sets the pixel to that color. We also have functions that can makeLighter(c) and makeDarker(c) an input color Last time we saw that we can also create colors: –makeColor(r,g,b) takes red, green, and blue values (in that order) between 0 and 255, and returns a Color object –pickAColor() lets you use a color chooser and returns the chosen color

Color:(108,86,142) Position: (12,9) x = 12 y = 9 red=108green=86blue=142 >>> pixel=getPixel(picture,12,9) >>> print pixel Pixel, red=108 green=86 blue=142 >>> value = getRed(pixel) >>> setRed (pixel, value+50) >>> setGreen(pixel, 0) >>> setBlue(pixel, getBlue(pixel)/2)

Color:(108,86,142) Position: (12,9) x = 12 y = 9 red=108green=86blue=142 >>> pixel=getPixel(picture,12,9) >>> print pixel Pixel, red=108 green=86 blue=142 >>> value = getRed(pixel) >>> setRed (pixel, value+50) >>> setGreen(pixel, 0) >>> setBlue(pixel, getBlue(pixel)/2) Color:(158,0,121) Position: (12,9) y = 9 red=158Green=0Blue=121

Color:(108,86,142) Position: (12,9) x = 12 y = 9 red=108green=86blue=142 >>> pixel=getPixel(picture,12,9) >>> print pixel Pixel, red=108 green=86 blue=142 >>> redValue = getRed(pixel) >>> greenValue = getGreen(pixel) >>> blueValue = getBlue(pixel) >>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2) >>> setColor(pixel, newColor)

Color:(158,0,121) Position: (12,9) x = 12 y = 9 red=158Green=0Blue=121 >>> pixel=getPixel(picture,12,9) >>> print pixel Pixel, red=108 green=86 blue=142 >>> redValue = getRed(pixel) >>> greenValue = getGreen(pixel) >>> blueValue = getBlue(pixel) >>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2) >>> setColor(pixel, newColor)

Repeating an action for all the pixels in a picture Example: for p in getPixels(picture): value = getRed(p) setRed(p, value*0.5)

Repeating an action for all the pixels in a picture decreaseRed() Example: def decreaseRed(picture): for p in getPixels(picture): value = getRed(p) setRed(p, value*0.5)

More examples: More examples (link) More examples (link) - you can copy and paste these to save time decreaseGreen() decreaseBlue() clearBlue() lighten() darken() negative() grayScale() 18

“Posterize” function For each pixel, if red<128, we set red=0, otherwise we set red=255 Similarly for green, blue

Assignment: Create a python function for each of the following: 1.Decrease red by 20% 2.Decrease green by 20% 3.Decrease blue by 20% 4.Increase red by 20%, if possible (i.e., if it does not exceed 255) 5.Similarly for increasing blue and green 6.“Posterize” 7.Think of another way to change an image and implement a function to do that 20