1
\$\begingroup\$

In a JPEG image, we may have thousands of distinct RGB values.
What is the process called of translating these numerous RGB values to just N distinct colour names/values?
(I'd like to know the name for the process so I could read on it).

I want to create a Python script that will get a JPEG as input and convert each pixel to only N colour names.
E.g., assume my colours are as in the table below.
I'd like to go over each pixel in the input image, find the RGB value in the table that is the closest to the current pixel's RGB value, and output for that pixel the corresponding colour name.

For instance, (220,0,12), (224,5,15), (223,1,10), and (216, 2,11) should all be translated to 'bright red'.

Colour R G B
medium blue 70 138 196
bright orange 229 28 23
transparent brown 165 144 129
transparent medium reddish-violet 236 155 193
bright yellowish-green 147 183 10
bright reddish-violet 154 0 96
transparent bright bluish-violet 155 148 198
silver 139 146 148
sand blue 93 115 138
sand yellow 139 115 81
earth blue 0 35 63
earth green 0 51 21
transparent flourescent blue 205 226 245
metallic dark grey 72 62 58
sand green 94 128 100
dark red 126 7 26
flame yellowish orange 242 153 0
transparent bright orange 235 117 13
reddish brown 90 27 11
medium stone grey 154 144 143
dark stone grey 75 92 85
light stone grey 227 227 217
light royal blue 133 190 232
bright purple 221 55 138
light purple 237 156 194
cool yellow 254 254 152
medium lilac 43 20 118
light nougat 243 191 135
phosph. green 252 250 211
warm gold 168 125 44
dark brown 46 13 4
transparent bright green 151 253 100
medium nougat 168 124 84
white 255 255 255
black 0 0 0
transparent yellow 248 238 104
transparent flourescent reddish-orange 229 100 70
transparent red 22 40 39
transparent light blue 181 223 233
transparent blue 78 175 230
transparent green 97 176 108
transparent flourescent green 242 238 92
transparent 240 240 240
bright yellow 255 197 2
bright red 220 0 12
bright blue 2 89 169
dark green 3 126 43
nougat 215 116 66
bright green 1 150 37
brick-yellow 218 188 125
dark orange 165 60 20
\$\endgroup\$
3
  • 2
    \$\begingroup\$ First things first, you'll need to clearly define what you mean by "closest" in some concrete way. There are a lot of factors to consider here. For example, do you prefer hue over saturation? brightness over hue? How do you resolve ties? Are you basing this on color perception or sheer numeric similarity? Is RGB even the best color space to use to achieve these goals? Beyond that it should be trivial to apply that function to a collection of pixel values. \$\endgroup\$ Commented Jun 20, 2022 at 5:43
  • \$\begingroup\$ Could you edit this to explain the photographic issue you're trying to solve here? At the moment, this reads like an abstract image editing problem, which would be off-topic here. Thanks! \$\endgroup\$
    – Philip Kendall
    Commented Jun 20, 2022 at 15:18
  • \$\begingroup\$ @Philip I want to convert a JPEG image to an image built from physical coloured squares. \$\endgroup\$
    – boardrider
    Commented Jun 22, 2022 at 1:39

3 Answers 3

6
\$\begingroup\$

The general term is color quantization.

When this is done on purpose for artistic effect, this is called posterization (hence the name of the filters in Gimp and Photoshop).

\$\endgroup\$
1
\$\begingroup\$

This is called indexed color. GIF uses indexed color up to 256 levels. You can set GIF's color levels to anything less than 256.

\$\endgroup\$
2
  • \$\begingroup\$ How would I apply indexed colour to a JPEG image? \$\endgroup\$
    – boardrider
    Commented Jun 20, 2022 at 5:20
  • 1
    \$\begingroup\$ @boardrider, in order to process the pixels, you'll need to render the jpeg image as actual pixel values, so the source format is not relevant to your algorithm, however, if you attempt to save the converted pixels as a jpeg, the compression algorithm will introduce artifacts, resulting in pixels not in your table \$\endgroup\$ Commented Jun 20, 2022 at 5:29
1
\$\begingroup\$

Actually, there are over 16 million colors possible in an 8-bit jpeg. And what you want to do can't be done easily as the encoded color space for a jpeg is Y'CbCr, not RGB... you would have to decode the jpeg file first using the appropriate color profile.

https://yasoob.me/posts/understanding-and-writing-jpeg-decoder-in-python/

But if what you want to do is convert an image using your own limited color table, that's pretty easy in photoshop...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.