你能用SIPS删除PNG中的alpha通道吗?

时间:2016-08-29 17:33:12

标签: macos terminal png transparency sips

从图像中删除alpha通道时,尝试在SIPS中保留 native 我熟悉ImageMagick中的过程:

convert -flatten test.png test-white.png

或:

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

但是当我在ss4Library上引用手册页时,它告诉我hasAlpa只有在我运行时才是布尔读取:

sips -g hasAlpha test.png

在标签下搜索并使用:

没有提到删除透明度的任何内容。你能用SIPS删除透明度吗?

3 个答案:

答案 0 :(得分:2)

使用ImageMagick或GraphicsMagick会更好,但是如果真的想要使用SIPS,您可以尝试通过将图像转换为BMP,然后再次回到PNG来删除透明度:

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

遗憾的是,您无法选择背景颜色,图像的透明部分将被替换为黑色。

答案 1 :(得分:2)

您可以使用以下小脚本,该脚本使用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行和第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);
...
...

我以同样的方式做了另一个答案here,以克服sips中另一个缺失的功能。

答案 2 :(得分:2)

另一种选择,比安装整个 ImageMagick 轻得多,可能是使用 NetPBM 套件:

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

您可以使用homebrew轻松安装 NetPBM

brew install netpbm
相关问题