SIPS能否移除PNG中的alpha通道?

4
尝试在SIPS中保持“本地”并从我熟悉的图像中删除alpha通道时,我知道在ImageMagick中的处理过程为:

convert -flatten test.png test-white.png

或者:

convert test.png -background white -alpha remove test.png

但是当我参考ss4Library上的手册时,它告诉我hasAlpha是一个只读布尔值,当我运行以下命令时:

sips -g hasAlpha test.png

在标签 下搜索并使用以下内容:

没有提到如何去除透明度。您能用SIPS去除透明度吗?

3个回答

3

使用ImageMagick或GraphicsMagick可能是更好的选择,但如果你真的想使用SIPS,你可以尝试通过将图像转换为BMP格式,然后再转回PNG格式来去除透明度:

sips -s format bmp input.png --out tmp.bmp
sips -s format png tmp.bmp --out output.png

很遗憾,您不能选择背景颜色,图像中透明的部分将被替换为黑色。


如果你有一个单独的像素,其透明度不正确需要修复,这将非常有用。 - user3069232

3
您可以使用以下小脚本,它使用了OSX内置的PHP,其中默认包括GD库,因此您可以保持原生状态,而无需安装ImageMagick或任何其他额外的内容:
#!/usr/bin/php -f

<?php
// Get start image with transparency
$src = imagecreatefrompng('start.png');

// Get width and height
$w = imagesx($src);
$h = imagesy($src);

// Make a blue canvas, same size, to overlay onto
$result = imagecreatetruecolor($w,$h);
$blue = imagecolorallocate($result,0,0,255);
imagefill($result,0,0,$blue);

// Overlay start image ontop of canvas
imagecopyresampled($result,$src,0,0,0,0,$w,$h,$w,$h);

// Save result
imagepng($result,'result.png',0);
?>

如果我从这个开始,中间是透明的:

enter image description here

我得到了这个结果:

enter image description here

我将画布背景设置为蓝色,以便您在 StackOverflow 的白色背景上看到它,但只需更改第 12 和 13 行以获取白色背景即可:

...
...
// Make a white canvas, same size, to overlay onto
$result = imagecreatetruecolor($w,$h);
$white = imagecolorallocate($result,255,255,255);
imagefill($result,0,0,$white);
...
...

我在同样的方向上又回答了另一个问题(这里),以克服sips中另一个缺失的功能。


2

另一种选择,比安装整个ImageMagick更轻量级的可能是使用NetPBM套件:

pngtopam -background=rgb:ff/ff/ff -mix start.png | pnmtopng - > result.png 

您可以使用 homebrew 轻松安装 NetPBM,命令如下:
brew install netpbm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接