Solution Found!
Use your drawFace function from the previous exercise to write a photo anonymizer. This
Chapter 6, Problem 16(choose chapter or problem)
Use your drawFace function from the previous exercise to write a photo anonymizer. This program allows a user to load an image file (such as a PPM or GIF) and to draw cartoon faces over the top of existing faces in the photo. The user first inputs the name of the file containing the image. The image is displayed and the user is asked how many faces are to be blocked. The program then enters a loop for the user to click on two points for each face: the center and somewhere on the edge of the face (to determine the size of the face). The program should then draw a face in that location using the drawFace function.
Hints: Section 4.8.4 describes the image-manipulation methods in the graphics library. Display the image centered in a GraphWin that is the same width and height as the image, and draw the graphics into this window. You can use a screen capture utility to save the resulting images.
Questions & Answers
QUESTION:
Use your drawFace function from the previous exercise to write a photo anonymizer. This program allows a user to load an image file (such as a PPM or GIF) and to draw cartoon faces over the top of existing faces in the photo. The user first inputs the name of the file containing the image. The image is displayed and the user is asked how many faces are to be blocked. The program then enters a loop for the user to click on two points for each face: the center and somewhere on the edge of the face (to determine the size of the face). The program should then draw a face in that location using the drawFace function.
Hints: Section 4.8.4 describes the image-manipulation methods in the graphics library. Display the image centered in a GraphWin that is the same width and height as the image, and draw the graphics into this window. You can use a screen capture utility to save the resulting images.
ANSWER:Step 1 of 3
The drawFace function takes the arguments containing the center coordinates, the size, and the window to draw the face. Using these arguments, the function draws the face as below:
def drawFace(center, size, window):
x1 = center.getX()
y1 = center.getY()
p1 = Point(x1-(.7 * size), y1 - size)
p2 = Point(x1+(.7 * size), y1 + size)
head = Oval(p1, p2)
head.setFill("white")
head.draw(window)
lc = Point(x1 - .2 * size, y1 + .6 * size)
rc = Point(x1 + .2 * size, y1 + .6 * size)
Eye1 = Circle(lc, .13 * size)
Eye1.setFill("white")
Eye1.draw(window)
Eye2 = Circle(rc, .13 * size)
Eye2.setFill("white")
Eye2.draw(window)
m1 = Point(x1 - .3 * size, y1 - .5 * size)
m2 = Point(x1 + .3 * size, y1 - .25 * size)
m3 = Point(x1 + .3 * size, y1 - .5 * size)
m = Rectangle(m1, m2)
m.setFill("white")
m.draw(window)
lLip = Line(m1, Point(x1- .3 * size, y1 - .25 * size))
mLCen = lLip.getCenter()
mLCx = mLCen.getX()
mLCy = mLCen.getY()
mRCen = Point(x1 + .3 * size, mLCy)
lip = Line(mLCen, mRCen)
lip.draw(window)