Upload
kya-woodhams
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
Python: Modifying Pictures Using Loops
Review
• JES command area – program area
• Defining/using functionsspecifying a sequence of steps for what the function should do.
• JES Functions for file manipulation JES->Files
• JES Functions for interacting with user JES-->Input/Output
• JES Functions for picture manipulation JES-->Pictures
Reminder: Manipulating Pictures
>>> pic1 = makeEmptyPicture(200,100)
>>> show(pic1)
>>> setAllPixelsToAColor(pic1, red)
>>> show(pic1)
>>> addText(pic1,30,50,“hello")
• similarly can add rectangles, lines, etc.
Reminder: Common errors
>>> file = pickAFile()>>> pic = makePicture(file)>>> show(file)The error was:sampleName not found globally.A local or global name could not be found. You need to define the function or variable before you try to use it in any way.>>> show(pic)>>> print picPicture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600
Oops!
Making new Colors• Some names for common colors are
predefined – try using the names: yellow, black, white, etc.
• makeColor() 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=108 green=86 blue=142
Encoding RGB
Colors go from (0,0,0) to (255,255,255)
>>> pic2 = makeEmptyPicture(200,100)>>> show(pic2)>>> seafoam = makeColor(153, 255, 204)>>> setAllPixelsToAColor(pic2, seafoam)>>> show(pic2)
Manipulating pixels
getPixel(picture,x,y)
gets a single pixel.
getPixels(picture)
gets all of them in an array
Color:(108,86,142) Position: (12,9)
x = 12
y = 9 red=108 green=86 blue=142
>>> pixel=getPixel(picture,12,9)>>> print pixelPixel, red=108 green=86 blue=142>>> allThePixels=getPixels(picture)>>> print allThePixels[0]Pixel red=191 green=149 blue=120
What can we do with a pixel?
• getRed(), getGreen(), and getBlue() are functions that take a pixel as input and return a value between 0 and 255
• setRed(), setGreen(), and setBlue() are functions that take a pixel as input and a value between 0 and 255
We can also get, set, and modify Colors
• getColor() takes a pixel as input and returns a Color object with the color at that pixel
• setColor() takes a pixel as input and a Color, then sets the pixel to that color
• We also have functions that can makeLighter() and makeDarker() an input color
• Last time we saw that we can also create colors:– makeColor() 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
Demonstrating: Manipulating Colors
>>> print getRed(pixel)168>>> setRed(pixel,255)>>> print getRed(pixel)255>>> color=getColor(pixel)>>> print colorcolor r=255 g=131 b=105>>> setColor(pixel,color)>>> newColor=makeColor(0,100,0)>>> print newColorcolor r=0 g=100 b=0>>> setColor(pixel,newColor)>>> print getColor(pixel)color r=0 g=100 b=0
>>> print colorcolor r=81 g=63 b=51>>> print newcolorcolor r=255 g=51 b=51>>> print colorcolor r=168 g=131 b=105>>> print makeDarker(color)color r=117 g=91 b=73>>> print colorcolor r=117 g=91 b=73>>> newcolor=pickAColor()>>> print newcolorcolor r=255 g=51 b=51
We can change pixels directly…
>>> file= pickAFile()>>> 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)
Media Tools
JES Picture function
OR
Media Tools menu
How do you find out what RGB values you have? And where?
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 picturedecreaseRed()
Example:
def decreaseRed(picture):
for p in getPixels(picture): value = getRed(p) setRed(p, value*0.5)
More examples:
• decreaseGreen()
• decreaseBlue()
• clearBlue()
• lighten()
• darken()
• negative()
• grayScale()
Saving to a file
• setMediaPath():Prompts the user to pick a folder on the computer. JES then will look for files in that directory unless given a full path, i.e. one that starts with "c:\"
• writePictureTo(picture, path):picture: the picture you want to be written out to a filepath: the path to the file you want the picture written toTakes a picture and a file name (string) as input, then writes the picture to the file as a JPEG.
• Example: writePictureTo(pic, “mypic.jpg”)
Assignment
Create picture manipulation functions that do the following:
1. Decrease red by 10%2. Decrease green by 10%3. Decrease blue by 10%4. Make the picture darker by decreasing red, green,
and blue by 10%5. Make a grayscale version of the image and then
negate it (B/W negative)
Next time:
• Repetition
• Conditionals
• Lists
• Menu-driven program
Repetition
for number in range(0,N):
pic = ....
filename = “pic” + str(number)
writePictureTo(pic, filename)
Conditionals
if age < 18:
showInformation(“Sorry, not allowed to vote yet.”)
else:
showInformation(“Please select candidate.”)
Lists
• [“hi”, “hello”, “howdy”, “hiya”, “yo”]
• [10, 23, 15]
• []
• range(0,6)
• range(1,4)
• range(3,8)
Preview of next assignmentCreate a menu-driven picture manipulation program that allows the user to make
various changes to images. Here is the algorithm:• welcome the user to your program • get the user to select a picture to work on • show(picture) • ask the user to select an option from a list of numbers:
– reduce red – reduce green – reduce blue – posterize – grayscale – <a feature of your choice>
• repaint(picture) • ask user whether to save the picture work, and if the response is “yes”, save it • show goodbye/thank you message