Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # U08_Ex14_Grayscale.py
- #
- # Author: Joseph Cloud
- # Course: Coding for OOP
- # Section: A3
- # Date: 15 Dec 2017
- # IDE: PyCharm Community Edition
- #
- # Assignment Info
- # Exercise: 14
- # Source: Python Programming
- # Chapter: 8
- #
- # Program Description
- # Converts a user entered image to grayscale
- # Algorithm (pseudocode)
- # Create the window
- # Introduction (wait for click to undraw)
- # Get filename from user
- # Display the image
- # Wait for the user to click
- # For each row in the image
- # For each column in the image
- # Set rgb values to rgb values of current pixel converted to grayscale
- # Set the current pixels color to new rgb values
- # Draw new image
- # Prompt the user for a filename to save the grayscale image as
- from graphics import *
- from tkinter.filedialog import askopenfilename
- def main():
- # Create the window
- window = GraphWin('Grayscale', 500, 500)
- # Introduction (wait for click to undraw)
- intro = Text(Point(250, 250), 'This Program Converts an Image to Grayscale (Click to Continue)')
- intro.draw(window)
- window.getMouse()
- intro.undraw()
- # Get filename from user
- fileName = askopenfilename()
- # Display the image
- pic = Image(Point(250, 250), fileName)
- pic.draw(window)
- # Wait for the user to click
- window.getMouse()
- # For each row in the image
- for y in range(pic.getHeight()):
- # For each column in the image
- for x in range(pic.getWidth()):
- # Set rgb values to rgb values of current pixel converted to grayscale
- r, g, b = pic.getPixel(x, y)
- brightness = int(round(.299 * r + .587 * g + .114 * b))
- # Set the current pixels color to new rgb values
- pic.setPixel(x, y, color_rgb(brightness, brightness, brightness))
- # Draw new image
- pic.undraw()
- pic.draw(window)
- window.getMouse()
- # Prompt the user for a filename to save the grayscale image as
- backdrop = Rectangle(Point(40, 220), Point(460, 160))
- backdrop.setFill('white')
- backdrop.setOutline('black')
- backdrop.draw(window)
- Text(Point(250, 175), 'Enter a filename to save this new image as').draw(window)
- newfilename = Entry(Point(250, 200), 40)
- newfilename.draw(window)
- window.getMouse()
- pic.save(newfilename.getText() + '.gif')
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment