Image Palletization & Color Analysis

Palletization

A core component of this project is to reduce the color complexity of an image so we can build color pallets with more manageable volumes of unique colors. Depending on the resolution, an image can easily have millions of unique color (RGB) combinations. To effectively analyze color trends we need to simplify the color complexity of an image down to something more manageable. That's where palletization comes in. This process simplifies the complexity of the image in a few ways.

I want to be able to manipulate the output of this process in Rhino with Grasshopper, so we will need to simplify both the color complexity of the image and the number of points exported as to avoid overloading Grasshopper. The "points" will be the centroid of the pooling cells where pixels data is pooled and summarized through this process…

Read image files

We will begin by giving Python a directory of images we want to analyze. The program reads over each file in the directory and checks if the file is an image. If the file is an image, the image is passed onto the following steps.

Resize the image

Since most of the images I'm working with are rather large, the size can be reduced. A user input will be requested at runtime where the user is prompted to enter a maximum dimension for the images. The user input value is passed as an argument into an image resizing function which resizes the image so the maximum dimension of the new does not exceed the user input. The image is uniformly scaled so the aspect ratio of the original image is maintained.

Pool color data

The resized image is then divided into an nxm grid, where n and m are calculated by taking the dimensions of the new image and passing them into a  function that determines the degree to which the image is simplified; more pooling cells will result in a higher fidelity output.

Summarize color data

The color values the pixels inside each pooling cell are pooled together and averaged to provide a single RGB code that best summarizes the dominant colors inside the pooling cell.

Palletization & Color Analysis

The diagram above shows two examples of images that have gone through the palletization and color quantification process. As noted previously, palletization allows us to reduce the number of colors we need to tabulate in a given image, from the tens of thousands, to a few hundred or even a few dozen depending on the parameters you set. The RGB space models on the left of the diagram show perspective and top-down views of the colors in the images, plotted in a 3D RGB space, where X = R, Y = B and Z = G.

Each dot represents a unique color in the palletized image, and its radius is representative of how dominant that color is in the image.

RGB Space with Grasshopper

For this exercise, I used Grasshopper to visualize the RGB space model with point clouds as described in the steps bellow.

  • 1: The Python node reads and parses the color attribute data written to JSON by the image palletization and segmentation scrips. The Python node outputs three lists, a, b and c, which contain the following data respectively:

    • a: List “a” contains the RGB codes of all unique colors in each image

    • b: List “b” contains the prevalence of each RGB code, measured by the percentage of pooling cells that match the RGB code in each image.

    • c: List “c” contains the name of each image.

    • The lists are meant to be used as relational tables where the index of each image and RGB value can be easily indexed and grouped.

  • 2: The image names from list “c” are reduced to a set of unique values by converting the list to a dictionary and taking the keys. A series of numbers are constructed within a given range determined by the input sliders and panels and are used to get image names at corresponding indexes. The image names at those indexes are grabbed using a “list item” node. This step was set up to select only data for specific images, but is not necessary for the general purpose of rendering the point-cloud.

  • 3: The image names are piped into a “member index” node that uses list comprehension to get the indexes of all rows in list “c” with the matching image name. These indexes are then piped into another set of “list item” nodes to get the RGB codes and percentages of colors belonging their respective images.

  • 4: The RGB codes are parsed and fed into an RGB node to create colors readable by Grasshopper. The RGB values are also piped into a “construct point” node, to generate the point-cloud.

  • 5: This step is not used in this example but this is where the greyscale value of each RGB code is calculated.

  • 6: The points and RGB colors are piped into a “dots'“ node and “tag” node, to render the point-cloud and generate text which displays the RGB code and its prevalence.

Visualizations

Next
Next

2D to 3D Image Transform