我正在制作一个用透明色去除背景颜色的脚本。
脚本就像这样,给出了正常的结果,区分了紫红色。
$val = 65535/40;
//divide by fuzz dilution, 1 is none
$val = floatval($val/0.9);
//create white border
$image->borderImage ( "rgb(255,255,255)" , 1 , 1 );
//make all white fill fuchsia
$image->floodFillPaintImage ( "rgb(255, 0, 255)" ,$val*3, "rgb(255,255,255)", 0 , 0, false);
//make fuchsia transparent
$image->paintTransparentImage("rgb(255,0,255)", 0.0, 0.5);
//remove border 1px that was added above
$image->shaveImage ( 1 , 1 );
但是,它会留下图像周围的颜色痕迹。这是一个例子,当我试图用白色背景去除手机周围的边框时 - 你可以清楚地看到边缘有白色痕迹。
问题是 - 当在像素0,0上进行泛光填充时,背景颜色变得错误,我需要一个"模糊"铲斗填充功能。 Imagemagic提供"模糊" floodFillPaintImage 的算法,但' fuzzy'的参数。部分仅用作像素的选择,而不是模糊着色。
例如,我有100%白色 - 算法正确选择完美的白色背景,并用新的,完美的紫红色图像填充它。当你设置"模糊"参数,算法正确选择 80%白色像素(例如),但再次使用100%紫红色对其进行着色。这就是丑陋角落出现问题的地方。
ImageMagic是否支持类似" true"模糊洪水填充和"真"模糊paintTransparentImage?或者有人对如何解决这个问题有更好的了解吗?
答案 0 :(得分:3)
我对此有所了解。我不能说我百分之百满意,但我已经解释了我在做什么并做了很小的步骤,所以你可以玩弄每个步骤并摆弄数字。为简单起见,我只是在命令行中执行了此操作。如果你达到了你想要的水平,它可以全部简化和加速。
#!/bin/bash
# Get size of original
sz=$(convert -format "%wx%h" phone.png info:)
# Floodfill background area with transparency
convert phone.png -fuzz 5% -fill none -draw 'color 0,0 floodfill' ObjectOnTransparent.png
# Extract alpha channel
convert ObjectOnTransparent.png -alpha extract Alpha.png
# Extract edges of alpha channel - experiment with thickness
convert Alpha.png -edge 1 AlphaEdges.png
# Get difference from background for all pixels
convert phone.png \( +clone -fill white -colorize 100% \) -compose difference -composite Diff.png
# Multiply edges with difference, so only edge pixels will have a chance of getting through to final mask
convert AlphaEdges.png Diff.png -compose multiply -composite EdgexDiff.png
# Extend Alpha by differences at edges
convert Alpha.png EdgexDiff.png -compose add -composite ReEdgedAlpha.png
# Apply new alpha to original image
convert phone.png \( ReEdgedAlpha.png -colorspace gray \) -compose copyopacity -composite RemaskedPhone.png
# Splat RemaskedPhone over red background
convert -size $sz xc:red RemaskedPhone.png -composite Result.png
<强> ObjectOnTransparent.png 强>
<强> Alpha.png 强>
<强> AlphaEdges.png 强>
<强> Diff.png 强>
<强> EdgexDiff.png 强>
<强> ReEdgedAlpha.png 强>
<强> Result.png 强>