[英]Can you remove the alpha channel in a PNG with SIPS?
您可以使用以下小腳本,它使用 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);
?>
所以,如果我從這個開始,中間是透明的:
我得到這個結果:
我將畫布背景設為藍色,以便您可以在 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
另一個缺失的功能。
使用 ImageMagick 或 GraphicsMagick 會是更好的主意,但如果您真的想使用 SIPS,您可以嘗試通過將圖像轉換為 BMP,然后再次返回 PNG 來消除透明度:
sips -s format bmp input.png --out tmp.bmp
sips -s format png tmp.bmp --out output.png
不幸的是,您不能選擇背景顏色,圖像的透明部分將被替換為黑色。
另一個比安裝整個ImageMagick輕得多的選擇可能是使用NetPBM套件:
pngtopam -background=rgb:ff/ff/ff -mix start.png | pnmtopng - > result.png
您可以使用homebrew
輕松安裝NetPBM :
brew install netpbm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.