3

画像に1色だけ残す(分離する)ことはできますか?現在、私は緑に興味があります:005d00

4

2 に答える 2

2

gdのimagecolorat()関数を使用できます。
すべてのピクセルを繰り返し処理し、それが目的の色であるかどうかを確認します。そうでない場合は、黒または白、あるいはそれを使用して実行したい色に設定します。

実例は次のとおりです。

function colorEquals($rgb_color, $hex_color)
{
    $r = ($rgb_color >> 16) & 0xFF;
    $g = ($rgb_color >> 8) & 0xFF;
    $b = $rgb_color & 0xFF;


    list($hr, $hg, $hb) = sscanf($hex_color, '%2s%2s%2s');
    $hr = hexdec($hr);
    $hg = hexdec($hg);
    $hb = hexdec($hb);

    return $r == $hr && $g == $hg && $b == $hb;
}

$width = 300;
$height = 300;

// create 300x300 image
$img = imagecreatetruecolor($width, $height);
// fill grey
$color = imagecolorallocate($img, 127, 127, 127);
imagefill($img, 0, 0, $color);

// set a square of pixels to 005d00
$your_color = imagecolorallocate($img, 0, 93, 0);
imagefilledrectangle($img, 10, 10, 100, 100, $your_color);

$white = imagecolorallocate($img, 255, 255, 255);

for($x = 0; $x < $width; ++$x)
{
    for($y = 0; $y < $height; ++$y)
    {
        $color = imagecolorat($img, $x, $y);
        if(!colorEquals($color, '005d00'))
        {
            // set it to white
            imagesetpixel($img, $x, $y, $white);
        }
    }
}

// output
header('Content-type: image/png');
imagepng($img);
于 2010-09-18T07:54:21.970 に答える
1

コマンドラインからImageMagickを使用してそれを行うことができます:

convert original.png -matte ( +clone -fuzz 1 -transparent #005d00 ) -compose DstOut -composite isolated.png

-fuzzコマンドは、色からパーセンテージの変化をとることができます。具体的な場合は、ファズを削除します。

()ブラケットは、bashシェル\(\)などでエスケープする必要があります。

于 2010-11-19T02:11:13.630 に答える