Joseph Drackey

Generation

generate functionTue, 28 Mar 2023

Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black

def posterize(image, rgb): img = image.copy() for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col, row) if p.getRed() < rgb[0]: r = 0 elif p.getRed() > rgb[0]: r = 255 else: r = p.getRed() if p.getGreen() < rgb[1]: g = 0 elif p.getGreen() > rgb[1]: g = 255 else: g = p.getGreen() if p.getBlue() < rgb[2]: b = 0 elif p.getBlue() > rgb[2]: b = 255 else: b = p.getBlue() newPixel = image.Pixel(r, g, b) img.setPixel(col, row, newPixel) return img

Questions about programming?Chat with your personal AI assistant