Download
python modifying pictures using loops n.
Skip this Video
Loading SlideShow in 5 Seconds..
Python: Modifying Pictures Using Loops PowerPoint Presentation
Download Presentation
Python: Modifying Pictures Using Loops

Python: Modifying Pictures Using Loops

723 Vues Download Presentation
Télécharger la présentation

Python: Modifying Pictures Using Loops

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Python: Modifying Pictures Using Loops

  2. Review • JES command area – program area • Defining/using functions specifying 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

  3. 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.

  4. Reminder: Common errors >>> file = pickAFile() >>> pic = makePicture(file) >>> show(file) The error was:sample Name 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 pic Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 Oops!

  5. 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

  6. red=108 green=86 blue=142 y = 9 Color:(108,86,142) Position: (12,9) x = 12

  7. 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)

  8. Manipulating pixels • getPixel(picture,x,y) • gets a single pixel. • getPixels(picture) • gets all of them in an array

  9. red=108 green=86 blue=142 y = 9 Color:(108,86,142) Position: (12,9) >>> pixel=getPixel(picture,12,9) >>> print pixel Pixel, red=108 green=86 blue=142 >>> allThePixels=getPixels(picture) >>> print allThePixels[0] Pixel red=191 green=149 blue=120 x = 12

  10. 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

  11. 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

  12. Demonstrating: Manipulating Colors >>> print getRed(pixel) 168 >>> setRed(pixel,255) >>> print getRed(pixel) 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) color r=0 g=100 b=0 >>> print color color r=81 g=63 b=51 >>> print newcolor color r=255 g=51 b=51 >>> print color color r=168 g=131 b=105 >>> print makeDarker(color) color r=117 g=91 b=73 >>> print color color r=117 g=91 b=73 >>> newcolor=pickAColor() >>> print newcolor color r=255 g=51 b=51

  13. 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)

  14. Media Tools How do you find out what RGB values you have? And where? JES Picture function OR Media Tools menu

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

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

  17. More examples: • decreaseGreen() • decreaseBlue() • clearBlue() • lighten() • darken() • negative() • grayScale()

  18. 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”)

  19. Assignment Create picture manipulation functions that do the following: • Decrease red by 10% • Decrease green by 10% • Decrease blue by 10% • Make the picture darker by decreasing red, green, and blue by 10% • Make a grayscale version of the image and then negate it (B/W negative)

  20. Next time: • Repetition • Conditionals • Lists • Menu-driven program

  21. Repetition for number in range(0,N): pic = .... filename = “pic” + str(number) writePictureTo(pic, filename)

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

  23. Lists • [“hi”, “hello”, “howdy”, “hiya”, “yo”] • [10, 23, 15] • [] • range(0,6) • range(1,4) • range(3,8)

  24. Preview of next assignment Create 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