How do I see the actual color of a single RGB value in Google Colab?

Viewed 72

Very basic question. I have a single vector (e.g., [53, 21, 110]) and I want to print the RGB color it represents in a colab notebook. Like a color swatch. What's the simplest way to do this?

1 Answers

The simplest way would be using the Image module from PIL. According to the documentation, you can construct an image with:

PIL.Image.new(mode, size, color=0)
  • mode [required]: determines the mode used for the image, it can be RGB, RGBA, HSV, etc. You can find more modes in the docs
  • size [required]: this is a tuple (weight, height) that represents the dimensions of your image in pixels.
  • color [optional]: this is the color of the image, it can receive a tuple to represent the RGB color in your case. The default color is black.

Then, to show the image within colab, you would use

display(img)

Given your question, the mode would need to be 'RGB' and if your vector is a list, you need to convert into a tuple to use it.

To show an 300px by 300px image, the code would look like.

from PIL import Image
img = Image.new('RGB', (300,300), color = (53, 21, 110))
display(img)

enter image description here

Related