如何使用Imagemagick将颜色与GIF的每一帧相乘

huangapple go评论58阅读模式
英文:

How to multiply a color over every frame of a gif using Imagemagick

问题

我一直在尝试编写一个ImageMagick命令,该命令接受一个输入的GIF,将每一帧与命令中指定的十六进制颜色(包括透明通道)进行复合乘法合成,然后保存为新的GIF。

目标是通过该颜色着色整个GIF,而透明背景变成目标颜色。我已经在PHP中使用Imagick库实现了这个功能,但在我的环境和PHP版本下速度太慢。

我尝试过像这样的命令:magick input.gif -coalesce \( -size "300x311" xc:"#DEADFF" -compose multiply) output.gif

以及其他变体,包括在括号外进行合成等。尺寸已硬编码为正确的尺寸。
我还尝试过使用convert,但是我尝试过的一切都没有奏效或者非常接近。

在PHP中的工作但速度慢的实现如下:

$targetColor = "#DEADFF";
$imagePath = "input.gif";
$image = new Imagick($imagePath);
$colorImage = new Imagick();
$colorImage->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel($targetColor));

foreach($image as $frame) {
        $frame->compositeImage($colorImage, Imagick::COMPOSITE_MULTIPLY, 0, 0, Imagick::CHANNEL_ALL);
}

header('Content-type: image/gif');
echo $image->getImagesBlob();

$image->clear();
$image->destroy();
英文:

I've been trying to write an imagemagick command that takes an input gif, composite multiplies every frame by a hex color specified in the command (on all channels including transparency), and then saves that new gif.

The goal is to tint the whole gif by the color, and the transparent background becomes the target color. I've gotten it working with the Imagick library in PHP, but it was too slow with my environment and PHP version.

I've tried commands like magick input.gif -coalesce \( -size "300x311"\ xc:"#DEADFF" -compose multiply) output.gif

As well as other variations, composing outside of the () and things like that. The size is hard coded to the correct sizes.
I've also tried using convert but nothing I've tried has worked or gotten very close.

The working, but slow, implementation in PHP is

$targetColor = "#DEADFF";
$imagePath = "input.gif";
$image = new Imagick($imagePath);
$colorImage = new Imagick();
$colorImage->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel($targetColor));

foreach($image as $frame) {
        $frame->compositeImage($colorImage, Imagick::COMPOSITE_MULTIPLY, 0, 0, Imagick::CHANNEL_ALL);
}

header('Content-type: image/gif');
echo $image->getImagesBlob();

$image->clear();
$image->destroy();

答案1

得分: 2

也许这个Imagemagick命令可以满足您的需求。您需要使用null:分隔符和-layers composite参数。

magick input.gif -coalesce null: \( -size "300x311" xc:"#DEADFF" \) -compose multiply -layers composite -layers optimize output.gif
英文:

Perhaps this will do what you want in Imagemagick. You need to use the null: separator and -layers composite.

magick input.gif -coalesce null: \( -size "300x311" xc:"#DEADFF" \) -compose multiply -layers composite -layers optimize output.gif

huangapple
  • 本文由 发表于 2023年7月14日 02:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定