Getting all pixels and all colors

How do we get to the structure that contains all pixels and all colors? The answer is simple but, as we'll see, less than optimal.

One way to map pixels to colors is to enumerate all pixels and all colors using the product() function:

xy = lambda xyp_c: xyp_c[0][0]p = lambda xyp_c: xyp_c[0][1]c = lambda xyp_c: xyp_c[1]distances = (    (xy(item), p(item), c(item), euclidean(p(item), c(item)))    for item in product(pixels, colors))

The core of this is the product(pixel_iter(img), colors) function that creates a sequence of all pixels combined with all colors. The overall expression then applies the euclidean() function to compute distances between pixels and Color objects. The result is a sequence of four tuples ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.