Joseph Drackey

Generation

generate functionWed, 29 Mar 2023

Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

def posterize(img, rgb_tuple): num_bins = len(rgb_tuple) img_mod = img.copy() for channel in range(3): channel_values = img[:,:,channel] for bin_index in range(num_bins): if bin_index == num_bins - 1: mask = channel_values >= rgb_tuple[bin_index] else: mask = np.logical_and(channel_values >= rgb_tuple[bin_index], channel_values < rgb_tuple[bin_index + 1]) img_mod[mask, channel] = rgb_tuple[bin_index] return img_mod

Questions about programming?Chat with your personal AI assistant