Guest User

Untitled

a guest
Dec 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. # U08_Ex14_Grayscale.py
  2. #
  3. # Author: Joseph Cloud
  4. # Course: Coding for OOP
  5. # Section: A3
  6. # Date: 15 Dec 2017
  7. # IDE: PyCharm Community Edition
  8. #
  9. # Assignment Info
  10. # Exercise: 14
  11. # Source: Python Programming
  12. # Chapter: 8
  13. #
  14. # Program Description
  15. # Converts a user entered image to grayscale
  16. # Algorithm (pseudocode)
  17. # Create the window
  18. # Introduction (wait for click to undraw)
  19. # Get filename from user
  20. # Display the image
  21. # Wait for the user to click
  22. # For each row in the image
  23. # For each column in the image
  24. # Set rgb values to rgb values of current pixel converted to grayscale
  25. # Set the current pixels color to new rgb values
  26. # Draw new image
  27. # Prompt the user for a filename to save the grayscale image as
  28.  
  29. from graphics import *
  30. from tkinter.filedialog import askopenfilename
  31.  
  32. def main():
  33. # Create the window
  34. window = GraphWin('Grayscale', 500, 500)
  35. # Introduction (wait for click to undraw)
  36. intro = Text(Point(250, 250), 'This Program Converts an Image to Grayscale (Click to Continue)')
  37. intro.draw(window)
  38. window.getMouse()
  39. intro.undraw()
  40. # Get filename from user
  41. fileName = askopenfilename()
  42. # Display the image
  43. pic = Image(Point(250, 250), fileName)
  44. pic.draw(window)
  45. # Wait for the user to click
  46. window.getMouse()
  47. # For each row in the image
  48. for y in range(pic.getHeight()):
  49. # For each column in the image
  50. for x in range(pic.getWidth()):
  51. # Set rgb values to rgb values of current pixel converted to grayscale
  52. r, g, b = pic.getPixel(x, y)
  53. brightness = int(round(.299 * r + .587 * g + .114 * b))
  54. # Set the current pixels color to new rgb values
  55. pic.setPixel(x, y, color_rgb(brightness, brightness, brightness))
  56. # Draw new image
  57. pic.undraw()
  58. pic.draw(window)
  59. window.getMouse()
  60. # Prompt the user for a filename to save the grayscale image as
  61. backdrop = Rectangle(Point(40, 220), Point(460, 160))
  62. backdrop.setFill('white')
  63. backdrop.setOutline('black')
  64. backdrop.draw(window)
  65. Text(Point(250, 175), 'Enter a filename to save this new image as').draw(window)
  66. newfilename = Entry(Point(250, 200), 40)
  67. newfilename.draw(window)
  68. window.getMouse()
  69. pic.save(newfilename.getText() + '.gif')
  70.  
  71.  
  72. if __name__ == '__main__':
  73. main()
Add Comment
Please, Sign In to add comment