[Image-SIG] Answer - Help colors
Oliver Albrecht
550283447739-0001 at t-online.de
Fri Jun 3 13:37:57 CEST 2005
Hello,
I not understand why nobody has answer me.
because you know that i can answer me self!?
>Oliver Albrecht wrote:
>
> - How discover the nearest color?
> I want make a script to switch the colorpalette from images.
>
> If it give another, better way tell it me. else i will post the
> completed script.
That is the request/answer:
if im_p.mode == "P":
lut = im_p.resize((16, 16))
lut.putdata(range(256))
lut = lut.convert("RGB")
palette1 = c_map.getdata()
palette2 = lut.getdata()
palette1 = list(palette1)
palette2 = list(palette2)
palette=set(palette1)
## not common colors from palette2
palette.difference_update(palette2)
ocolors=set(palette2)
## not common colors from palette1
ocolors.difference_update(palette1)
## give the colors a index
palette = [(palette1.index(color), color) for color in palette]
for (cr,cg,cb) in ocolors:
opal_dict = {}
for item in palette:
r,g,b=item[1]
opal_dict[abs(r-cr)+abs(g-cg)+abs(b-cb)]=item[0]
## the nearest color
palette2[palette2.index((cr,cg,cb))]=
palette1[opal_dict[min(opal_dict.keys())]]
for colorID in xrange(len(palette2)):
palette2[colorID]=palette1.index(palette2[colorID])
im_p.putpalette(c_map_pl)
im_p=im_p.point(palette2) ##PIL(book)Returns a copy of the image
#where each pixel has been mapped
#through the given table.
> I want make a Mask from RGB Image from only one color .
> Thats my try(has putdata() lesser performance?):
[..snipped..]
> Is that efficient enough?
i mean this is better:
def MakeMask(im):
data_list=[]
bground=im.getpixel((0,0))
for r, g, b in im.getdata():
if (r,g,b) != bground: i=0
else: i=255
data_list.append(i)
im = Image.new('1',im.size)
im.putdata(data_list)
return im
--
<!--Olliminatore-->
More information about the Image-SIG
mailing list