Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. from Cimpl import *
  2. import math
  3. file = choose_file()
  4.  
  5. image = load_image(file)
  6.  
  7. def grayscale(image: Image) -> Image:
  8. """Return a grayscale copy of image.
  9.  
  10. >>> image = load_image(choose_file())
  11. >>> gray_image = grayscale(image)
  12. >>> show(gray_image)
  13. """
  14. new_image = copy(image)
  15. for x, y, (r, g, b) in image:
  16.  
  17. # Use the pixel's brightness as the value of RGB components for the
  18. # shade of gray. These means that the pixel's original colour and the
  19. # corresponding gray shade will have approximately the same brightness.
  20.  
  21. brightness = (r + g + b) // 3
  22.  
  23. # or, brightness = (r + g + b) / 3
  24. # create_color will convert an argument of type float to an int
  25.  
  26. gray = create_color(brightness, brightness, brightness)
  27. set_color(new_image, x, y, gray)
  28. #show (new_image)
  29. return new_image
  30.  
  31.  
  32. def Sepia_channel():
  33.  
  34. """
  35. The funcion returns the selected image in a sepia filter, however it does not alter the original image.
  36.  
  37. Ahmed Shakir - 101143951
  38.  
  39. """
  40. sepia_picture = grayscale(image)
  41.  
  42. for x, y, (r,g,b) in sepia_picture:
  43. if g < 63:
  44. b*=0.9
  45. r*=1.1
  46. b= round (b)
  47. r = round (r)
  48. elif 63 <= g <= 191:
  49. r*=1.15
  50. b *= 0.85
  51. r = round (r)
  52. b = round (b)
  53. elif g > 191 :
  54. b*= 0.93
  55. r*=1.08
  56. b = round (b)
  57. r= round (r)
  58. sepia = create_color( r, g, b)
  59. set_color(sepia_picture, x, y, sepia)
  60.  
  61. show(sepia_picture)
  62. return sepia_picture
  63.  
  64.  
  65.  
  66. def test_filter_grayscale (image):
  67. gray_picture = grayscale (image)
  68.  
  69. for x,y, (r,g,b) in gray_picture:
  70.  
  71. r, g, b = get_color (gray_picture, x, y)
  72. red, green, blue = get_color (image, x, y)
  73. brightness = (red + green + blue)// 3
  74. if brightness== r and brightness == g and brightness == b:
  75. continue
  76. else:
  77. return False
  78. return True
  79.  
  80.  
  81. def test_filter_sepia (image):
  82.  
  83. filtered_picture = Sepia_channel()
  84. graypic = grayscale (image)
  85.  
  86.  
  87. graytest = test_filter_grayscale (image)
  88.  
  89. if graytest == False:
  90. return False
  91.  
  92. for x,y, (r,g,b) in filtered_picture:
  93.  
  94. r,g,b = get_color (filtered_picture, x, y)
  95. red, green, blue = get_color (graypic, x, y)
  96. if red< 63 and r== round(red*1.1) :
  97. continue
  98. elif (red >= 63 and red<=191) and r == round(red*1.15):
  99. continue
  100. elif red>191 and r == round(red*1.08):
  101. continue
  102. else:
  103. return False
  104.  
  105. if blue< 63 and b== round(blue*0.9) :
  106. continue
  107. elif (blue >= 63 and blue<=191) and b == round(blue*0.85):
  108. continue
  109. elif blue>191 and b == round(blue*0.93):
  110. continue
  111. else:
  112. return False
  113. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement