MACL mil
MACL mil

Reputation: 1

Defining analogue (similar) colors

I want to do a program that take the color from a image, and than give me the analogue colors..., but I don't know how I can use the color taken from the image because it doesn't give me the colors like the object...

I tried to make like this:

from PIL import Image

img = Image.open("C:/Marco/programas/orange2.jpg").convert("P")

colors = []

for cor_rgb in img.getpalette():
    if cor_rgb not in colors:
        colors.append(cor_rgb)
c = Image.Image.getdata(img)

print(c)
print (colors)

Can anybody help me please?

Upvotes: 0

Views: 49

Answers (2)

martineau
martineau

Reputation: 123541

You can use specify an adaptive conversion when you use the Image.convert() method. The details aren't given but "adaptive" in this context usually means the set colors that best represents those in the image.

After doing the conversion, the value returned by Image.getpalette() is a list of RGB colors representing each of the colors the adaptive algorithm chose to put into the palette. From the looks of the results using my test image, it looks like the colors are in the table ordered by popularity (but I'm not sure and it's not documented).

Professional image editing programs like Adobe Photoshop®, have several options when converting to a paletted image, including "selective", "uniform", and "perceptual". I often use the latter because it takes into consideration the fact that the human eye can't detect differences between all color equally, it using some metric to measure "perceptual color difference".

from PIL import Image

image_filename = "oranges2.jpg"
img = Image.open(image_filename).convert("P", palette=Image.ADAPTIVE)
palette = img.getpalette()
colors = [palette[i:i+3]  # Split palette entries up into groups of 3.
            for i in range(0, len(palette), 3)]

print(len(colors))
for color in colors:
    print(color)

Sample output:

256
[250, 212, 69]
[253, 194, 17]
[252, 185, 22]
[254, 185, 1]
[255, 177, 10]
[255, 177, 2]
[255, 177, 0]
[254, 177, 2]
[240, 177, 35]
[255, 167, 10]
[255, 168, 4]
[255, 167, 2]
...
[115, 15, 1]
[98, 19, 1]
[98, 11, 1]
[86, 16, 2]
[75, 15, 2]
[83, 7, 1]
[72, 7, 1]
[58, 12, 3]
[56, 6, 1]
[59, 3, 1]
[48, 3, 1]
[36, 8, 3]
[36, 2, 1]
[20, 6, 3]
[18, 2, 1]

Here's the image file I used for testing:

enter image description here

Upvotes: 1

furas
furas

Reputation: 143097

Documentation for getpalette shows that it gives it as flat list [R,G,B, ... ]. Using numpy.array you could reshape it to array [ [R,G,B], [...], ...] and get unique elements

from PIL import Image
import numpy

img = Image.open("image.jpg").convert("P")

colors = numpy.array(img.getpalette())
colors = colors.reshape((-1,3))
colors = numpy.unique(colors, axis=0)

print(colors)

Example result

[[  0   0   0]
 [  0   0  51]
 [  0   0 102]
 [  0   0 153]
 [  0   0 204]
 [  0   0 255]
 [  0  51   0]
 [  0  51  51]
 [  0  51 102]
 [  0  51 153]
 [  0  51 204]
 [  0  51 255]
 [  0 102   0]
 [  0 102  51]
 [  0 102 102]
 [  0 102 153]
 [  0 102 204]
 [  0 102 255]

But I don't know how to define "analogue colors"

Upvotes: 0

Related Questions