user238469
user238469

Reputation:

Assign scalar value to 'RGB' of few dominant colors of an image using python

I have an image which is made up of some discrete colors such as green, blue, red, yellow, navy. I would like to read this image and make an array which has following two attributes:

  1. one numerical value for each discrete color,
  2. size of each axis of the array to be proportionate (approximately) to the image dimensions.

P.S.: All the questions that I have come across with respect to reading images deal with rgb/pixels. This is just one question that would have been useful if the objective of the question was reversed, in which case it may have been similar to the question I am asking.

Upvotes: 0

Views: 726

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 208003

My other answer is getting a bit too long and I have had some more thoughts I wanted to share... look at this answer as a pre-processing step before the other one maybe.

If you convert your image in the HSL colorspace, like this:

convert image.png -colorspace hsl -separate hsl.png

You will get the Hue in file hsl-0.png like this:

enter image description here

and the Saturation in file hsl-1.png like this:

enter image description here

You can see that the Saturation image is lighter where your saturated red, green, blue and yellow colours are and darker where the brown is that you are not interested in. You can also see that the colours are fairly well differentiated in the Hue image. That leads me to the idea of using the Saturation channel as a mask and the Hue channel to split into colours...

convert image.png -colorspace hsl -channel s -threshold 80% -channel h -colors 12 -colorspace rgb result.png

enter image description here

Or, starting with your original image, you could go directly with this:

convert image.png -fx "u.saturation>0.8?u:0" -median 3 result.png

enter image description here

You could increase the -median 3 to 5 to remove more speckles.

So, I would say my current best shot at this is as follows:

# Make a swatch of the colours want in the output image
convert xc:black xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png

# Clean up image using Saturation to select coloured areas, median to reduce speckles, then remap to swatch created above and resample down to 50x20
convert image.png -fx "u.saturation>0.8?u:0" -median 3 -remap swatch.png -sample 50x20! result.png

enter image description here

Now print the palette indices - which is what you actually asked for - I know how to do this easily in PHP:

#!/usr/local/bin/php
<?php
// Read in a palettised image
$im = imagecreatefrompng("result.png");

for ($r = 0; $r < imagesy($im); $r++) {
    printf("Row %d: ",$r);
    for ($c = 0; $c < imagesx($im); $c++) {
        $pixel = imagecolorat($im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 0 0 0 0 0 0 0 0 0 4
Row 19: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 208003

Not sure if this is close to what you want, but if I use ImageMagick which is installed on most Linux distros and available for OSX and Windows, I can run this command in the Terminal:

convert image.png -alpha off -colors 6 -sample 50x20! result.png

enter image description here

And, if I ask for the output in text, rather than as an image, I get this:

convert image.png -alpha off -colors 6 -sample 50x20! txt:

# ImageMagick pixel enumeration: 50,20,255,srgb
0,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
1,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
2,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
3,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
4,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
5,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
6,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
7,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
8,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
9,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
10,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
11,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
12,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
13,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
14,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
15,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
16,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
17,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
18,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
19,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
20,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
21,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
22,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
23,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
24,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
25,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
26,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
27,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
28,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
29,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
30,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
31,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
32,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
33,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
34,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
35,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
36,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
37,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
38,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
39,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
40,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
41,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
42,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
43,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
44,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
45,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
46,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
47,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
48,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
49,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
0,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
1,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
2,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
3,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
4,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
5,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
6,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
7,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
8,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
9,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
10,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
11,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
12,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
13,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
14,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
15,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
16,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
17,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
18,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
19,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
20,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
21,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
22,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
23,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
24,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
25,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
26,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
27,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
28,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
29,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
30,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
31,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
32,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
33,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
34,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
35,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
36,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
37,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
38,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
39,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
40,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
41,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
42,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
43,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
44,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
45,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
46,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
47,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
48,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
49,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
0,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
1,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
2,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
3,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
4,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
5,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
6,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
7,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
8,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
9,2: (47517,15011,17755)  #B93A45  srgb(185,58,69)
10,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
11,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
12,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
13,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
14,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
15,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
16,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
17,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
18,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
19,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
20,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
21,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
22,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
23,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
24,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
25,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
26,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
27,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
28,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
29,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
30,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
31,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
32,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
33,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
34,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
35,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
36,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
37,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
38,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
39,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
40,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
41,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
42,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
43,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
44,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
45,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
46,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
47,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
48,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
49,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
0,3: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
1,3: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
...
...
46,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
47,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
48,18: (17144,20406,18979)  #434F4A  srgb(67,79,74)
49,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
0,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
1,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
2,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
3,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
4,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
5,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
6,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
7,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
8,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
9,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
10,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
11,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
12,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
13,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
14,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
15,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
16,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
17,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
18,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
19,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
20,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
21,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
22,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
23,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
24,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
25,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
26,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
27,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
28,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
29,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
30,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
31,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
32,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
33,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
34,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
35,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
36,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
37,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
38,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
39,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
40,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
41,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
42,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
43,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
44,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
45,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
46,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
47,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
48,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
49,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)

Maybe this will get you started...

Adding -median 7 after -colors 6 decreases the small "noisy" patches somewhat.

convert image.png -alpha off -colors 6 -median 7 -sample 50x20! result.png

enter image description here

Note that there are Python bindings for ImageMagick too.

Another option may be to make your own little colour swatch of the 6 colours you want identified like this:

convert xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,160,120)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png

and then to tell ImageMagick to map all the colours in the image to the swatch:

convert image.png -quantize sRGB -alpha off -remap swatch.png  -sample 50x20! result.png

enter image description here

If you want the palette of colours used, add -verbose info: at the end instead of the output image name, then you will get this - look at the last 6 lines - they show the indices of the colours:

convert image.png -alpha off -colors 6 -sample 50x20! -type palette -verbose info:

Image: image.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 50x20+0+0
  Resolution: 59.05x59.05
  Print size: 0.84674x0.338696
  Units: PixelsPerCentimeter
  Type: Palette
  Base type: Palette
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8/16-bit
  Channel depth:
    red: 16-bit
    green: 16-bit
    blue: 16-bit
  Channel statistics:
    Pixels: 1000
    Red:
      min: 2 (0.00935378)
      max: 204 (0.801602)
      mean: 114.15 (0.447646)
      standard deviation: 85.674 (0.335977)
      kurtosis: -1.77949
      skewness: -0.174985
      entropy: 0.994611
    Green:
      min: 58 (0.229053)
      max: 180 (0.706386)
      mean: 127.02 (0.498117)
      standard deviation: 45.2218 (0.17734)
      kurtosis: -1.4666
      skewness: -0.2851
      entropy: 0.994611
    Blue:
      min: 64 (0.249165)
      max: 175 (0.687266)
      mean: 107.26 (0.420626)
      standard deviation: 43.1734 (0.169307)
      kurtosis: -1.48194
      skewness: 0.477601
      entropy: 0.994611
  Image statistics:
    Overall:
      min: 2 (0.00935378)
      max: 204 (0.801602)
      mean: 116.143 (0.455463)
      standard deviation: 61.2345 (0.240135)
      kurtosis: -1.14278
      skewness: -0.180352
      entropy: 0.994611
  Colors: 6
  Histogram:
       146: (  2,144,105) #029069 srgb(2,144,105)
       150: ( 10,125,175) #0A7DAF srgb(10,125,175)
       199: ( 67, 79, 74) #434F4A srgb(67,79,74)
       148: (185, 58, 69) #B93A45 srgb(185,58,69)
       200: (198,172,154) #C6AC9A srgb(198,172,154)
       157: (204,180, 64) #CCB440 srgb(204,180,64)
  Colormap entries: 6
  Colormap:
         0: ( 67, 79, 74) #434F4A srgb(67,79,74)      <--- index 0 in palette
         1: (185, 58, 69) #B93A45 srgb(185,58,69)     <--- index 1 in palette
         2: (  2,144,105) #029069 srgb(2,144,105)
         3: (204,180, 64) #CCB440 srgb(204,180,64)
         4: (198,172,154) #C6AC9A srgb(198,172,154)
         5: ( 10,125,175) #0A7DAF srgb(10,125,175)    <--- last palette entry

Upvotes: 1

Related Questions