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

查看:36
本文介绍了如何修剪动画 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 的正确组合来使用单个 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.

有没有更简单的方法来修剪 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.

现在您可以轻松地将这些尺寸和偏移量放入扭曲视口设置中并进行无操作扭曲.删除用于获取测量值的克隆平面,+repage"其余部分,并完成您需要的任何其他 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天全站免登陆