http://pastebin.com/v0B3Vje2 画像からピクセルを取得し、別のプログラムでそれに最も近い色を見つける方法を探しています(「別のプログラム」のソースにコンパイルできます。完璧な場合ソースインジェクションなしで互換性があります)そしてその色を使用して正しいピクセルに配置します。基本的に、Script / Code / Executableは、たとえば画像ファイルを取得し、最も一致する各ピクセルを再作成します。私が話しているプログラムはThePowderToyです。(powdertoy.co.uk)。「パブリックセーブ」にはCGIを含めることができないため、ご存知の場合は、私的な目的と概念実証に使用しています。そこでのユーザーの1人であるJoJoBondは、最初に行ったように、これを行うことが許可されています。
2 に答える
1
Python Imaging Libraryを使用して、画像を読み込み、ピクセルの色の値を抽出できます。
import Image
img = Image.open('random.png')
width, height = img.size
pixels = img.getdata()
print 'pixels:'
for i, px in enumerate(img.getdata()):
# decide whether to replace this pixel
# call out to external program to translate color value
r, g, b = px
npx = (b, g, r)
# replace pixel with new color value
y = i / width
x = i % width
img.putpixel((x, y), npx)
print px, npx
出力:
pixels:
(58, 0, 0) (0, 0, 58)
(0, 0, 0) (0, 0, 0)
(0, 0, 4) (4, 0, 0)
(0, 0, 0) (0, 0, 0)
(0, 0, 0) (0, 0, 0)
(0, 245, 0) (0, 245, 0)
(0, 0, 0) (0, 0, 0)
(0, 0, 0) (0, 0, 0)
(14, 0, 0) (0, 0, 14)
...
于 2011-04-28T14:08:57.540 に答える
1
おそらくscipy.cluster.vq.vqを使用して画像を量子化します:
import numpy as np
import scipy.cluster.vq as vq
import Image
import random
img = Image.open('cartoon.png').convert('RGB')
arr = np.asarray(img)
shape_orig = arr.shape
# make arr a 2D array
arr = arr.reshape(-1,3)
# create an array of all the colors in the image
palette=np.unique(arr.ravel().view([('r',np.uint8),('g',np.uint8),('b',np.uint8)]))
# randomly select 50 colors from the palette
palette=palette[random.sample(range(len(palette)),50)]
# make palette a 2D array
palette=palette.view('uint8').reshape(-1,3)
# Quantize arr to the closet color in palette
code,dist=vq.vq(arr,palette)
arr_quantized=palette[code]
# make arr_quantized have the same shape as arr
arr_quantized=arr_quantized.reshape(shape_orig)
img_new=Image.fromarray(arr_quantized)
img_new.save('/tmp/cartoon_quantized.png')
cartoon.png を使用:
上記のコードは、cartoon_quantized.png を生成します。
注: 近い色を定義する最善の方法については、私はよく知りません。
上記のコードはvq.vq
、指定された画像の色とのユークリッド距離が最も小さいパレットの色を選択するために使用します。RGBタプルでユークリッド距離を使用することが、近い色を定義する最良の方法であるかどうかはわかりません.
RGB とは異なるカラー システムを選択したい場合や、ユークリッド距離とは異なるメトリックを選択したい場合もあります。残念ながら、vq.vq
ユークリッド距離とは異なるメトリックが必要な場合に使用できるかどうかはわかりません...
于 2011-04-28T15:52:36.413 に答える