Visualizing the Color Spaces of Images with Python and MatPlotLib
By: Lior Ben-David
Tags: Python, Code, Visualization, Matplotlib, Colors

Alright, now let's start actually working with python. First, we are going to install our necessary libraries. Run the following command to install MatPlotLib, the library we will be using to plot our colors.

python -m pip install -U matplotlib

Next, we need to install Pillow, a Python library that lets us decompose images

pip install Pillow

Now let’s import everything we need at the top of our python file:

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

from PIL import Image

Now let’s load in the image you want to breakdown with Pillow.

im = Image.open("PUT YOUR IMAGE PATH HERE")

For this example, I’m going to be using this nice photo:

Next, we are going to create a 2D array of all the pixels in the image

px = im.load()

im.load() returns us a 2D array storing each pixel. Each pixel object is an array of 3 integers representing the RGB values of that specific pixel. I recommend that you play around with this px value to best understand how it is storing the pixels.

Next, let's set up our 3D space. MatPlotLib makes this incredibly easy. First, we are going to create the axes of our graph which we will specify to be 3D.

ax = plt.axes(projection = '3d')

To plot our color points, MatPlotLib requires us to give it 4 arrays, representing the x,y,z coordinates of all our points, as well as the color for each of the points. Let’s make these arrays, which will be empty for now.

x = []

y = []

z = []

c = []

Now let’s populate these arrays. To do so, we are going to have to iterate through all our pixels, adding the corresponding values to our x,y,z, and color arrays.

for row in range(0,im.height):

for col in range(0, im.width):

pix = px[col,row]

x.append(pix[0])

y.append(pix[1])

z.append(pix[2])

c.append((pix[0] / 255, pix[1] / 255, pix[2] / 255))

Note that MatPlotLib requires that we normalize the RGB values between 0 and 1 when providing colors, so we divide each RGB value by 255. Also, note that this code will often result in the arrays containing duplicates, which is going to make MatPlotLib do more work than necessary. To stop this, we can add an if statement so that it only adds the color if it is not already in the array.

for row in range(0,im.height):

for col in range(0, im.width):

pix = px[col,row]

newCol = (pix[0] / 255, pix[1] / 255, pix[2] / 255)



if(not newCol in c):

x.append(pix[0])

y.append(pix[1])

z.append(pix[2])

c.append(newCol)

Finally, we just need to plot these points, which we can do with MatPlotLib:

ax.scatter(x,y,z, c = c)

plt.show()

Upon running this python script with the following image:

We get the following plot:

And there we go! Now for review, our final code is:

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

from PIL import Image im = Image.open("YOUR IMAGE PATH")

px = im.load() ax = plt.axes(projection = '3d') x = []

y = []

z = []

c = [] for row in range(0,im.height):

for col in range(0, im.width):

pix = px[col,row]

newCol = (pix[0] / 255, pix[1] / 255, pix[2] / 255)



if(not NewCol in c):

x.append(pix[0])

y.append(pix[1])

z.append(pix[2])

c.append(newCol) ax.scatter(x,y,z, c = c)

plt.show()

Hope you enjoyed this project! I recommend playing around with different images and seeing what patterns emerge among these color plots.

Here are some other plots that I made from different images: