2016-10-08 3 views
2

の背景色だけを交換してください私のコードです:はここPNG

#! /usr/bin/env sh 
# Generate test image. 
convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 55,55" in.png 

# Make background transparent. 
convert in.png -fill none -draw 'matte 0,0 floodfill' -flop -draw 'matte 0,0 floodfill' -flop out.png 
# Replace transparent background with green. 
mogrify -background green -flatten out.png 

# The wrong way. 
convert in.png -transparent blue oops.png 
mogrify -background green -flatten oops.png 

それは、このスニペットに基づいています:https://snippets.aktagon.com/snippets/558-how-to-remove-a-background-with-imagemagick

はこれで起動する:

Black circle filled with blue, blue background

私が取得したいですこれは:

Black circle filled with blue, green background

ないこの:

Black circle filled with green, green background

私が代わりにmogrify続いconvertの単一convertコマンドでこれを達成することはできますか?

私はImageMagick 6.8.9-9を使用しています。

答えて

3

基本的に、あなたはこのように、を "FLOODFILL" 求めている:左上のピクセル(0,0)を見て、すべて同様に着色画素を記入します

convert in.png -fill green -draw 'color 0,0 floodfill' result.png 

enter image description here

をそれには緑色のが接続されています。あなたの背景に多少のばらつきがあるとします。それはJPEGです、あなたのサークルには上部と下部の縁に触れていた場合、それは図の右側に周りの氾濫から塗りつぶしを妨げることをいくつか曖昧度

convert in.jpg -fuzz 25% ... 

注意を追加します。それでは、あなたはこのようなあなたのサークルを作成していたとしましょう:

convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 50,0" in.png 

enter image description here

そして、あなたは上記のコマンドを実行して、あなたが取得します:

enter image description here

をそのような場合は、 1つのピクセル幅の境界線を色の末尾に追加して、「flow」を最初に通過させてからflood-fillし、最後に後で取り除くことができます。

convert in.png -bordercolor blue -border 1 -fill green -draw 'color 0,0 floodfill' -shave 1x1 result.png 
+0

色の代わりに透明度を塗りつぶすには、 'convert in.png -fill none -draw 'color 0,0 floodfill' result.png'を使用します。 – Erik