I'm looking for a way of converting an image so that all non-transparent pixels (those that have alpha != 1) into black and transparent pixels untouched (or converted to white). The closest I got was with the below imagemagick command:
我正在寻找一种方法来转换图像,使所有非透明像素(具有alpha != 1的像素)都变成不受影响的黑色和透明像素(或转换为白色)。我最接近的是下面的imagemagick命令:
convert <img> -colorspace Gray <out>
However this still gives me some gray colors instead of a complete black. I have tried all colorspace options and none does the job.
但是这仍然给了我一些灰色而不是完全的黑色。我尝试过所有的颜色空间选项,但是没有一个可以。
Any idea how I could achieve this with imagemagick or with similar tools (or with a PHP library if it exists)
我可以用imagemagick或者类似的工具(如果有PHP库的话)来实现这一点吗?
3 个解决方案
#1
19
I know this question is old, but now I've stumbled on it I might as well answer it.
我知道这个问题由来已久,但现在我偶然发现了它,我不妨回答它。
The ImageMagick command you want is:
您需要的ImageMagick命令是:
convert <img> -alpha extract -threshold 0 -negate -transparent white <out>
I'll breakdown what it's doing as well.
我也会分析它在做什么。
-
-alpha extract
- Take the alpha mask of the image. Completely transparent pixels will be black, completely opaque ones white. - -alpha提取-获取图像的alpha蒙版。完全透明的像素是黑色的,完全不透明的像素是白色的。
-
-threshold 0
- Increase all channels to their maximum value if they are greater than zero. In this case, it will make every pixel white except the ones that are completely black. - -阈值0 -如果大于零,则增加所有通道的最大值。在这种情况下,它会使每个像素都变成白色,除了那些完全是黑色的像素。
-
-negate
- Invert the image. Now our blacks are white and our whites are black. - -反相-反相图像。现在我们的黑人是白人,我们的白人是黑人。
-
-transparent white
- Set white pixels to be transparent. This can be excluded if you'd prefer your originally transparent pixels to be white. - -透明白色-设置为透明的白色像素。如果您希望您最初的透明像素是白色的,那么可以排除这种情况。
Before
After
#2
1
I'm not sure whether it'll help you (i.e. whether the methods presented will leave the transparent pixels alone) but check out the answers to this question: PHP/ImageMagic Get the “shadow” of an image
我不确定它是否会对您有所帮助(例如,所提供的方法是否会保留透明像素),但请检查这个问题的答案:PHP/ImageMagic获得图像的“阴影”
#3
1
Well, you could do it with GD and a pair of loops:
你可以用GD和一对环来做:
$img = imagecreatefromstring(file_get_contents($imgFile));
$width = imagesx($img);
$hieght = imagesy($img);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $width; $y++) {
$color = imagecolorat($img, $x, $y);
$color = imagecolorforindex($color);
if ($color['alpha'] == 1) {
imagesetpixel($img, $x, $y, $black);
} else {
imagesetpixel($img, $x, $y, $white);
}
}
}
Or, you could replace colors (this may or may not work):
或者,你可以更换颜色(这可能有用,也可能没用):
$img = imagecreatefromstring(file_get_contents($imgFile));
$maxcolors = imagecolorstotal($img);
for ($i = 1; $i <= $maxcolors; $i++) {
$color = imagecolorforindex($i);
if ($color['alpha'] == 1) {
imagecolorset($img, $i, 0, 0, 0);
} else {
imagecolorset($img, $i, 255, 255, 255);
}
}
#1
19
I know this question is old, but now I've stumbled on it I might as well answer it.
我知道这个问题由来已久,但现在我偶然发现了它,我不妨回答它。
The ImageMagick command you want is:
您需要的ImageMagick命令是:
convert <img> -alpha extract -threshold 0 -negate -transparent white <out>
I'll breakdown what it's doing as well.
我也会分析它在做什么。
-
-alpha extract
- Take the alpha mask of the image. Completely transparent pixels will be black, completely opaque ones white. - -alpha提取-获取图像的alpha蒙版。完全透明的像素是黑色的,完全不透明的像素是白色的。
-
-threshold 0
- Increase all channels to their maximum value if they are greater than zero. In this case, it will make every pixel white except the ones that are completely black. - -阈值0 -如果大于零,则增加所有通道的最大值。在这种情况下,它会使每个像素都变成白色,除了那些完全是黑色的像素。
-
-negate
- Invert the image. Now our blacks are white and our whites are black. - -反相-反相图像。现在我们的黑人是白人,我们的白人是黑人。
-
-transparent white
- Set white pixels to be transparent. This can be excluded if you'd prefer your originally transparent pixels to be white. - -透明白色-设置为透明的白色像素。如果您希望您最初的透明像素是白色的,那么可以排除这种情况。
Before
After
#2
1
I'm not sure whether it'll help you (i.e. whether the methods presented will leave the transparent pixels alone) but check out the answers to this question: PHP/ImageMagic Get the “shadow” of an image
我不确定它是否会对您有所帮助(例如,所提供的方法是否会保留透明像素),但请检查这个问题的答案:PHP/ImageMagic获得图像的“阴影”
#3
1
Well, you could do it with GD and a pair of loops:
你可以用GD和一对环来做:
$img = imagecreatefromstring(file_get_contents($imgFile));
$width = imagesx($img);
$hieght = imagesy($img);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $width; $y++) {
$color = imagecolorat($img, $x, $y);
$color = imagecolorforindex($color);
if ($color['alpha'] == 1) {
imagesetpixel($img, $x, $y, $black);
} else {
imagesetpixel($img, $x, $y, $white);
}
}
}
Or, you could replace colors (this may or may not work):
或者,你可以更换颜色(这可能有用,也可能没用):
$img = imagecreatefromstring(file_get_contents($imgFile));
$maxcolors = imagecolorstotal($img);
for ($i = 1; $i <= $maxcolors; $i++) {
$color = imagecolorforindex($i);
if ($color['alpha'] == 1) {
imagecolorset($img, $i, 0, 0, 0);
} else {
imagecolorset($img, $i, 255, 255, 255);
}
}