def test_greyscale(self): self.assertEqual(91.36, utilities.get_greyscale(100, 100, 100)) self.assertEqual(0.0, utilities.get_greyscale(0, 0, 0)) self.assertEqual(67.095, utilities.get_greyscale(255, 0, 113)) self.assertEqual(232.968, utilities.get_greyscale(255, 255, 255)) self.assertEqual(168.93599999999998, utilities.get_greyscale(165, 189, 201))
def accent_color(input_path: str, output_path: str, h: float, _range: int): def h1_lt_h2(_h, _h1, _h2): return _h1 < _h < _h2 def h1_gt_h2(_h, _h1, _h2): return _h <= _h1 or _h >= _h2 h1 = (h - (_range / 2) + 360) % 360 h2 = (h + (_range / 2) + 360) % 360 func = h1_lt_h2 if h1 <= h2 else h1_gt_h2 input_image = utilities.Image(input_path) width, height = input_image.get_size() output_image = utilities.create_empty_image(width, height) output_pixels = output_image.load() for x in range(width): for y in range(height): hsv = utilities.rgb_to_hsv(*input_image.pixels[x, y]) if func(hsv.h, h1, h2): output_pixels[x, y] = input_image.pixels[x, y] else: greyscale_value = int(utilities.get_greyscale(*input_image.pixels[x, y])) output_pixels[x, y] = (greyscale_value, greyscale_value, greyscale_value) output_image.save(output_path)
def make_sepia(image_path: str, dest_path: str, factor: int): image = Image(image_path) width, height = image.get_size() output = create_empty_image(width, height) output_pixels = output.load() for x in range(width): for y in range(height): red, green, blue = image.pixels[x, y] grey_red = int(get_greyscale(red, green, blue)) grey_green = int(get_greyscale(red, green, blue)) grey_blue = int(get_greyscale(red, green, blue)) output_pixels[x, y] = (grey_red + 2 * factor, grey_green + factor, grey_blue) output.save(dest_path)
def __init__(self, image_path: str, destination_path: str, factor: int): self.min_factor = 0 self.max_factor = int(get_greyscale( 255, 255, 255)) # max greyscale valaue for #FFFFFF if not self.min_factor < factor < self.max_factor: raise ValueError( f"Factor value should be from 0 to {self.max_factor}") self.image = Image(image_path) self.factor = factor self.destination_path = destination_path # raw error table and output image self.width, self.height = self.image.get_size() self.greyscale_image = self.image2greyscale() # error size + 1 than input image because of lack of if statements self.error_table = [[0 for _ in range(self.height + 2)] for __ in range(self.width + 2)] self.output_image = PillowImage.new("RGB", (self.width, self.height), self.WHITE) self.output_pixels = self.output_image.load() self.output_image_name = "output_floyd_steinberg.jpg"
def image2greyscale(self): greyscale_image = PillowImage.new("RGB", (self.width, self.height), self.WHITE) pixels = greyscale_image.load() for x in range(self.width): for y in range(self.height): grey_value = int(get_greyscale(*self.image.pixels[x, y])) pixels[x, y] = (grey_value, grey_value, grey_value) greyscale_image.save(os.path.join(self.destination_path, "output_greyscale.jpg")) return greyscale_image