Python Programming: An Introduction to Computer Science, 3rd Ed.
Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
bartleby

Videos

Question
Book Icon
Chapter 6, Problem 16PE
Program Plan Intro

Draw a face

Program plan:

  • Import the header file.
  • Define the “drawFace” method
    • Get the “x” and “y” positions
    • Get the “p1” and “p2” positions
    • Call the “Oval” method for drawing the face
    • Set the color to the face
    • Draw the face
    • Get the “lc” and “rc” position
    • Call the “Circle” method
    • Set the color to the face
    • Call the “draw” method
    • Call the “Circle” method
    • Set the color to the face
    • Call the “draw” method
    • Get the “m1”, “m2”, and “m3” values
    • Call the “Rectangle” method
    • Set the color to the face
    • Call the “draw” method
    • Call the “Line” method
    • Get the center position
    • Get the “x” and “y” position
    • Call the “Point” method
    • Call the “Line” method
    • Call the “draw” method
    • Get the “x” and “y” position for different variables
    • Calculate “t” and “t1” value
    • Call the “Rectangle” method
    • Call the “draw” method
    • Iterate “i” until it reaches 7
      • Clone the teeth by calling “clone” method
      • Move the face by calling “move” method
      • Call the “draw” method
  • Define the main method.
    • Read the filename from the user
    • Get the image width and height from the file
    • Set the graph win size
    • Set the coords for graph
    • Draw the face
    • Get the number of faces from the user
    • Iterate “i” until it reaches “n” value
      • Get the mouse action
      • Call the “drawFace” method
  • Call the function “main()”.

Expert Solution & Answer
Check Mark
Program Description Answer

This Python program is used to get the GIF (Graphic Interchange format) file and draw the smileys when the user clicking the GIF image.

Explanation of Solution

Program:

#import the header file

from graphics import *

#definition of "drawFace" method

def drawFace(center, size, window):

    #get the "x" value

    x1 = center.getX()

    #get the "y" value

    y1 = center.getY()

    #get the "p1" value

    p1 = Point(x1-(.7 * size), y1 - size)

    #get the "p2" value

    p2 = Point(x1+(.7 * size), y1 + size)

    #call the "Oval" method

    head = Oval(p1, p2)

    #fill the color

    head.setFill("white")

    #call the "draw" method

    head.draw(window)

    #get the "lc" value

    lc = Point(x1 - .2 * size, y1 + .6 * size)

    #get the "rc" value

    rc = Point(x1 + .2 * size, y1 + .6 * size)

    #call the "Circle" method

    leftEye = Circle(lc, .13 * size)

    #fill the color

    leftEye.setFill("white")

    #call the "draw" method

    leftEye.draw(window)

    #call the "Circle" method

    rightEye = Circle(rc, .13 * size)

    #fill the color

    rightEye.setFill("white")

    #call the "draw" method

    rightEye.draw(window)

    #get the "m1" value

    m1 = Point(x1 - .3 * size, y1 - .5 * size)

    #get the "m2" value

    m2 = Point(x1 + .3 * size, y1 - .25 * size)

    #get the "m3" value

    m3 = Point(x1 + .3 * size, y1 - .5 * size)

    #call the "Rectangle" method

    mouth = Rectangle(m1, m2)

    #fill the color

    mouth.setFill("white")

    #call the "draw" method

    mouth.draw(window)

    #call the "Line" method

    leftLip = Line(m1, Point(x1- .3 * size, y1 - .25 * size))

    #get the center position

    mLeftCent = leftLip.getCenter()

    #get the "x" position

    mLCx = mLeftCent.getX()

    #get the "y" position

    mLCy = mLeftCent.getY()

    #call the "Point" method

    mRightCent = Point(x1 + .3 * size, mLCy)

    #call the "Line" method

    lip = Line(mLeftCent, mRightCent)

    #call the "draw" method

    lip.draw(window)

    #get the "x" positions

    m1x = m1.getX()

    m3x = m3.getX()

    #get the "y" positions

    m1y = m1.getY()

    m2y = m2.getY()

    #calculate the "t" value

    t = m1x - m3x

    #calculate the "t1" value

    t1 = Point(m1x - (1/8 * t), m2y)

    #call the "Rectangle" method

    teeth = Rectangle(m1, t1)

    #call the "draw" method

    teeth.draw(window)

    #iterate "i" until it reaches 7

    for i in range (7):

        #clone the face

        t2 = teeth.clone()

        #call the "move" method

        t2.move(-i * (1/8 * t), 0)

        #call the "draw" method

        t2.draw(window)

#definition of main method

def main():

    #get the input filename from the user

    fname = input("Enter filename: ")

    infile = Image(Point(10, 10), fname)

    #get the width of the image

    wWidth = infile.getWidth()

    #get the height of the image

    wHeight = infile.getHeight()

    #set graph win name

    window = GraphWin('Smile!', wWidth,wHeight)

    #set the coords

    window.setCoords(0, 0, 20, 20)

    #draw a smiley

    infile.draw(window)

    #get how many faces the user wants to draw

    n = eval(input("How many faces should we block? "))

    #iterate "i" until it reaches "n"

    for i in range(n):

        #get the mouse action

        point = window.getMouse()

        #draw a face by calling the method "drawFace"

        drawFace(point, 3, window)

#call the "main" method

main()

Sample Output

Output:

Enter filename: test_img.gif

How many faces should we block? 2

>>>

Screenshot of “Smile!” window

Python Programming: An Introduction to Computer Science, 3rd Ed., Chapter 6, Problem 16PE

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Note : please avoid using AI answer the question by carefully reading it and provide a clear and concise solutionHere is a clear background and explanation of the full method, including what each part is doing and why. Background & Motivation Missing values: Some input features (sensor channels) are missing for some samples due to sensor failure or corruption. Missing labels: Not all samples have a ground-truth RUL value. For example, data collected during normal operation is often unlabeled. Most traditional deep learning models require complete data and full labels. But in our case, both are incomplete. If we try to train a model directly, it will either fail to learn properly or discard valuable data. What We Are Doing: Overview We solve this using a Teacher–Student knowledge distillation framework: We train a Teacher model on a clean and complete dataset where both inputs and labels are available. We then use that Teacher to teach two separate Student models:  Student A learns…
Here is a clear background and explanation of the full method, including what each part is doing and why. Background & Motivation Missing values: Some input features (sensor channels) are missing for some samples due to sensor failure or corruption. Missing labels: Not all samples have a ground-truth RUL value. For example, data collected during normal operation is often unlabeled. Most traditional deep learning models require complete data and full labels. But in our case, both are incomplete. If we try to train a model directly, it will either fail to learn properly or discard valuable data. What We Are Doing: Overview We solve this using a Teacher–Student knowledge distillation framework: We train a Teacher model on a clean and complete dataset where both inputs and labels are available. We then use that Teacher to teach two separate Student models:  Student A learns from incomplete input (some sensor values missing). Student B learns from incomplete labels (RUL labels missing…
here is a diagram code : graph LR subgraph Inputs [Inputs] A[Input C (Complete Data)] --> TeacherModel B[Input M (Missing Data)] --> StudentA A --> StudentB end subgraph TeacherModel [Teacher Model (Pretrained)] C[Transformer Encoder T] --> D{Teacher Prediction y_t} C --> E[Internal Features f_t] end subgraph StudentA [Student Model A (Trainable - Handles Missing Input)] F[Transformer Encoder S_A] --> G{Student A Prediction y_s^A} B --> F end subgraph StudentB [Student Model B (Trainable - Handles Missing Labels)] H[Transformer Encoder S_B] --> I{Student B Prediction y_s^B} A --> H end subgraph GroundTruth [Ground Truth RUL (Partial Labels)] J[RUL Labels] end subgraph KnowledgeDistillationA [Knowledge Distillation Block for Student A] K[Prediction Distillation Loss (y_s^A vs y_t)] L[Feature Alignment Loss (f_s^A vs f_t)] D -- Prediction Guidance --> K E -- Feature Guidance --> L G --> K F --> L J -- Supervised Guidance (if available) --> G K…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
C - File I/O; Author: Tutorials Point (India) Ltd.;https://www.youtube.com/watch?v=cEfuwpbGi1k;License: Standard YouTube License, CC-BY
file handling functions in c | fprintf, fscanf, fread, fwrite |; Author: Education 4u;https://www.youtube.com/watch?v=aqeXS1bJihA;License: Standard Youtube License