Turn all mostlyred pixels blue mostlyred means within a distance of 140 of red Hints use distance getColor setColor def redToBlue pic add 3 more lines ID: 784411
Download The PPT/PDF document "Python Practice !!! 1 Easy Picture Func..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Python Practice !!!
1
Slide2Easy Picture Function
# Turn all mostly-red pixels blue ("mostly-red" means within a distance of 140 of red)
#
Hints: use “distance”, “getColor”, “setColor”def redToBlue (pic): # add 3 more lines for p in getPixels (pic): if distance (getColor (p), red) < 140: setColor (p, blue)
2
Slide3Medium Picture Function
# turn bottom-right corner of a picture (100x100 pixels) white
#
Hints: use “getWidth”, “getHeight”, “getPixelAt”, “setColor”def whiteCorner (pic): # add 4 more lines for x in range (getWidth(pic)-100,getWidth(pic)): for y in range (getHeight(pic)-100,getHeight(pic)):
p
=
getPixelAt(pic, x, y) setColor (p, white)
3
Slide4Easy “Miscellaneous” Function
# Count from 0 to 100 by 5s
def
countAndPrint (): # 2 more lines 4 for i in range (0, 101, 5): print i
Slide5Conversion Practice
Write a function that displays in the command area (but does not return) the letter grade corresponding to a number grade (90 and up = A, etc.; no + or – grades).
def
showGrade (n): if (n >= 90): print "A" if (n >= 80) and (n < 90): print "B" if (n >= 70) and (n < 80): print "C" if (n >= 60) and (n < 70): print "D" if (n < 60): print "F" Write code that calls your function two times with grades 99 and 71. showGrade (99)
showGrade
(71)
Slide6Picture size practice
Write a function to return the number of pixels in “pic”
def
numPixels (pic): width = getWidth (pic) height = getHeight(pic) return width*height6
Slide7Is a picture “big”?
Write a function to return “true” if the picture is “big” (has over 90,000 pixels) (use the function you wrote on the previous slide)
def
isBig (pic): if numPixels (pic) > 90000: return true return false7
Slide8Mystery Function
# What do I do?
def
mystery (pic): for x in range (30,70): for y in range (0, 30): px1 = getPixel (pic, x, y) copyColor = getColor (px1) px2 = getPixel (pic, x, y+30) setColor (px2, copyColor)Copies the rectangle bounded by (30,0) and (70,30) immediately below itself.
8
Slide9# Code to turn the
centered bottom
half of a picture red
9def bottomHalfRed(pic): for x in range(getWidth(pic)/4,getWidth(pic)*3/4): for y in range(getHeight
(pic
)/2,
getHeight(pic)): p = getPixel(pic, x, y)
setColor
(p
, red)
Slide10Generalize the previous function to allow it to change the bottom half of a picture to any color, not just red. E.g., we want to be able to type the following in the command area:
bottomHalfRed3 (
myPic
, green)bottomHalfRed3 (yourPic, blue)10def bottomHalfRed3(pic,color): for x in range(getWidth(pic)/4,getWidth(pic)*3/4): for y in range(getHeight(pic)/2, getHeight(pic)): p = getPixel(pic, x, y)
setColor
(p, color)
Slide11# Code to make a copy of a picture and turn the bottom half of the copy red (use “
duplicatePicture
()”
def bottomHalfRedCopy (pic): 11