2011-12-28 10 views

答えて

4

これはピクセル単位で行われ、画像内の任意の数の色に対して機能します(ただし、多くの色や大きな画像では遅くなることがあります)。また、パレット画像(それを変換する)にも使用できます。

import Image 

def color_separator(im): 
    if im.getpalette(): 
     im = im.convert('RGB') 

    colors = im.getcolors() 
    width, height = im.size 
    colors_dict = dict((val[1],Image.new('RGB', (width, height), (0,0,0))) 
         for val in colors) 
    pix = im.load()  
    for i in xrange(width): 
     for j in xrange(height): 
      colors_dict[pix[i,j]].putpixel((i,j), pix[i,j]) 
    return colors_dict 

im = Image.open("colorwheel.tiff") 
colors_dict = color_separator(im) 
#show the images: 
colors_dict.popitem()[1].show() 
colors_dict.popitem()[1].show() 
  1. 色数が指定できる最大値を(超えない限り、タプルとして、im.getcolors()戻っ画像内のすべての色のリストと、それらが発生する回数を呼び出すと、デフォルト〜256)。
  2. 次に、イメージ内の色と空のイメージの対応する値をキーにして辞書colors_dictを作成します。
  3. 次に、すべてのピクセルの画像を繰り返し、各ピクセルの適切な辞書エントリを更新します。このようにすると、画像を1度だけ読む必要があることを意味します。画像を読み取るときにピクセルアクセスを高速化するためにload()を使用します。
  4. color_separator()は、イメージのすべての固有の色でキー入力されたイメージの辞書を返します。

それはより速くあなたがcolors_dict内のすべての画像のためのload()を使用することができますが、画像は多くの色を持っているし、大きい場合には、大量のメモリを消費する可能性があるので、あなたは少し注意する必要があるかもしれないようにします。これが問題でない場合、(colors_dictの作成後に)追加:

fast_colors = dict((key, value.load()) for key, value in colors_dict.items()) 

とスワップ:

colors_dict[pix[j,i]].putpixel((j,i), pix[j,i]) 

用:

fast_colors[pix[j,i]][j,i] = pix[j,i] 

22色の画像:enter image description here

22色分解画像:

あなたは22枚の画像、たとえば、黄色と赤のもののいくつかを組み合わせただろうか210だから、

enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here enter image description hereenter image description hereenter image description hereenter image description hereenter image description here enter image description hereenter image description hereenter image description hereenter image description here

+0

、 ?または、少なくとも22枚の画像のうちのいくつかを新しい画像に貼り付けることができるように、画像に黒がないのですか? – klocey