SHARE
TWEET

Untitled

a guest Nov 16th, 2019 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from Cimpl import *
  2. import math
  3.  
  4. file = choose_file()
  5.  
  6. image = load_image(file)
  7.  
  8.  
  9. def grayscale(image: Image) -> Image:
  10.     """Return a grayscale copy of image.
  11.  
  12.     >>> image = load_image(choose_file())
  13.     >>> gray_image = grayscale(image)
  14.     >>> show(gray_image)
  15.     """
  16.     new_image = copy(image)
  17.     for x, y, (r, g, b) in image:
  18.         # Use the pixel's brightness as the value of RGB components for the
  19.         # shade of gray. These means that the pixel's original colour and the
  20.         # corresponding gray shade will have approximately the same brightness.
  21.  
  22.         brightness = (r + g + b) // 3
  23.  
  24.         # or, brightness = (r + g + b) / 3
  25.         # create_color will convert an argument of type float to an int
  26.  
  27.         gray = create_color(brightness, brightness, brightness)
  28.         set_color(new_image, x, y, gray)
  29.         # show (new_image)
  30.     return new_image
  31.  
  32.  
  33. def Sepia_channel():
  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. def test_filter_grayscale(image):
  66.     gray_picture = grayscale(image)
  67.  
  68.     for x, y, (r, g, b) in gray_picture:
  69.  
  70.         r, g, b = get_color(gray_picture, x, y)
  71.         red, green, blue = get_color(image, x, y)
  72.         brightness = (red + green + blue) // 3
  73.         if brightness == r and brightness == g and brightness == b:
  74.             continue
  75.         else:
  76.             return False
  77.     return True
  78.  
  79.  
  80. def test_filter_sepia(image):
  81.     filtered_picture = Sepia_channel()
  82.     graypic = grayscale(image)
  83.  
  84.     graytest = test_filter_grayscale(image)
  85.  
  86.     if graytest == False:
  87.         return False
  88.  
  89.     for x, y, (r, g, b) in filtered_picture:
  90.  
  91.         r, g, b = get_color(filtered_picture, x, y)
  92.         red, green, blue = get_color(graypic, x, y)
  93.         if red < 63 and r == round(red * 1.1):
  94.             print("before else 1")
  95.             continue
  96.         elif (red >= 63 and red <= 191) and r == round(red * 1.15):
  97.             print("before else 2")
  98.             continue
  99.         elif red > 191 and r == round(red * 1.08):
  100.             continue
  101.             print("before else 3")
  102.         # else:
  103.         #     print("I failed here")
  104.         #     issue = True
  105.         #
  106.         # print("here now")
  107.         elif blue < 63 and b == round(blue * 0.9):
  108.             print("I can get here too!")
  109.             continue
  110.         elif (63 <= blue <= 191) and b == round(blue * 0.85):
  111.             continue
  112.         elif blue > 191 and b == round(blue * 0.93):
  113.             continue
  114.         else:
  115.             return False
  116.     return True
  117.  
  118.  
  119. print(test_filter_sepia(image))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top