如何修剪动画gif(使用imagemagick)? [英] How to trim animated gif (using imagemagick)?

查看:770
本文介绍了如何修剪动画gif(使用imagemagick)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个纯色背景颜色的GIF动画

Given an animated gif over a solid background color

我想修剪远离填充。具体来说,我想在所有帧上将图像裁剪到前景对象的最大范围:

I'd like to trim away the padding. Concretely, I'd like to crop the image to the maximum extent of the foreground object over all frames:

我似乎无法找到 -alpha -background <的正确组合/ code>使用单个 convert 命令实现此目的。例如,如果我发出

I can't seem to find the right combination of -alpha, -background to achieve this with a single convert command. For example, if I issue

convert -dispose 2 input.gif -trim -layers TrimBounds fail.gif

我得到个别修剪范围小于最大范围的帧的随机背景颜色所有框架:

I get random "background" colors for frames whose individual trimmed extents are smaller than the maximum extent over all frames:

我可以使用一长串命令实现正确的输出:

I can achieve the correct output with a long string of commands:

convert input.gif -trim -layers TrimBounds out-%03d.miff
mogrify -background "rgb(20%,30%,80%)" -layers flatten out-*.miff
convert out-*.miff output.gif
rm out-*.miff

这很慢,写了一堆临时文件,并要求我知道背景颜色( rgb(20%,30%,80%))明确。

This is slow, writes a bunch of temporary files, and requires me to know the background color ("rgb(20%,30%,80%)") explicitly.

有没有更简单的方法修剪一个动画ted gif?

Is there a simpler way to trim an animated gif?

相关问题考虑显式裁剪而不是自动修剪。

This related question considers explicit cropping rather than automatic trimming.

推荐答案

你可以使用IM的-distort和定义的视口完成这种修剪。

You can accomplish this sort of trimming using IM's "-distort" with a defined viewport.

convert oHBWq.gif -coalesce +repage -background none \
   \( -clone 0--1 -trim -flatten -trim \) \
   -set option:distort:viewport %[fx:u[-1].w]x%[fx:u[-1].h]+%[fx:u[-1].page.x]+%[fx:u[-1].page.y] \
   -delete -1 -distort SRT 0 +repage output.gif

克隆输入帧,单独修剪它们,并使它们保持原始对齐以展平它们。然后它再次修剪那个扁平的,以摆脱多余的透明背景。结果将是正确的大小,并为完成的图像具有正确的页面偏移。您不必知道背景颜色。

That clones the input frames, trims them individually, and flattens them keeping their original alignment. Then it trims that flattened one again to get rid of the excess transparent background. The result will be the right size and have the correct page offsets for the finished images. You don't have to know the background color.

现在,您可以轻松地将这些尺寸和偏移量设置为扭曲视口设置并执行无操作扭曲。删除用于获取测量值的克隆扁平滤波器,+重新调整其余部分,然后使用您需要的任何其他GIF设置完成。

Now you can easily get those dimensions and offsets into a distort viewport setting and do a no-op distort. Delete the cloned flattened one that was used to get the measurements, "+repage" the rest, and finish with whatever other GIF settings you need.

这篇关于如何修剪动画gif(使用imagemagick)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆