import numpy from typing import List, Tuple Color = Tuple[int, int, int] Pixels = List[List[Color]] class Picture: def __init__(self, pixels: Pixels = None): self.pixels = numpy.array(pixels) def clear(self): self.pixels[:, :, :] = 0 def get_height(self): return self.pixels.shape[0] def get_width(self): return self.pixels.shape[1] def compare(self, other: "Picture") -> float: return numpy.sum((self.pixels - other.pixels)**2) def write_ppm(self, path: str): with open(path, 'wb') as ppmfile: ppmfile.write(b"P6\n") ppmfile.write("{} {} 255\n".format( self.get_width(), self.get_height() ).encode('utf-8')) ppmfile.write((self.pixels * 255.0).astype(numpy.uint8).tobytes()) def blend_rect(self, x: int, y: int, w: int, h: int, r: float, g: float, b: float, alpha: float): ph = self.get_height() pw = self.get_width() upper_y = min(ph, y + h) upper_x = min(pw, x + w) color = (r * alpha, g * alpha, b * alpha) # what does this line mean? new_colors = self.pixels[y: upper_y, x: upper_x] + color # why clip? self.pixels[y: upper_y, x: upper_x] = new_colors.clip(0, 1)