windows 从一组 jpeg 图像创建动画 gif
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3688870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create animated gif from a set of jpeg images
提问by Cbox
I need something that can be scripted on windows 7. This image will be used in banners.
我需要一些可以在 Windows 7 上编写脚本的东西。这个图像将用于横幅。
回答by dwurf
Simon P Stevens' answeralmost got me there:
西蒙 P 史蒂文斯的回答几乎让我到了那里:
ffmpeg -f image2 -i image%d.jpg video.avi
ffmpeg -i video.avi -pix_fmt rgb24 -loop_output 0 out.gif
Let's see if we can neaten this up.
让我们看看我们是否可以把它整理好。
Going via an avi is unnecessary. A -pix_fmt
of rgb24
is invalid, and the -loop_output
option prevents looping, which I don't want. We get:
通过 avi 是不必要的。A -pix_fmt
ofrgb24
无效,该-loop_output
选项可防止循环,这是我不想要的。我们得到:
ffmpeg -f image2 -i image%d.jpg out.gif
My input pictures are labeled with a zero-padded 3-digit number and I have 30 of them (image_001.jpg, image_002.jpg, ...), so I need to fix the format specifier
我的输入图片标有零填充的 3 位数字,其中有 30 张(image_001.jpg、image_002.jpg、...),所以我需要修复格式说明符
ffmpeg -f image2 -i image_%003d.jpg out.gif
My input pictures are from my phone camera, they are way too big! I need to scale them down.
我输入的图片来自我的手机相机,它们太大了!我需要缩小它们的规模。
ffmpeg -f image2 -i image_%003d.jpg -vf scale=531x299 out.gif
I also need to rotate them 90 degrees clockwise
我还需要将它们顺时针旋转 90 度
ffmpeg -f image2 -i image_%003d.jpg -vf scale=531x299,transpose=1 out.gif
This gif will play with zero delay between frames, which is probably not what we want. Specify the framerate of the input images
此 gif 将在帧之间以零延迟播放,这可能不是我们想要的。指定输入图像的帧率
ffmpeg -f image2 -framerate 9 -i image_%003d.jpg -vf scale=531x299,transpose=1 out.gif
The image is just a tad too big, so I'll crop out 100 pixels of sky. The transpose makes this tricky, I use the post-rotated x and y values:
图像有点大,所以我将裁剪出 100 像素的天空。转置使这变得棘手,我使用旋转后的 x 和 y 值:
ffmpeg -f image2 -framerate 9 -i image_%003d.jpg -vf scale=531x299,transpose=1,crop=299,431,0,100 out.gif
The final result - I get to share my mate's awesome facial expression with the world:
最终结果 - 我可以与全世界分享我的伴侣令人敬畏的面部表情:
回答by Simon P Stevens
You can do this with ffmpeg
你可以用ffmpeg做到这一点
First convert the images to a video:
首先将图像转换为视频:
ffmpeg -f image2 -i image%d.jpg video.avi
(This will convert the images from the current directory (named image1.jpg, image2.jpg...) to a video file named video.avi.)
(这会将图像从当前目录(名为 image1.jpg、image2.jpg...)转换为名为 video.avi 的视频文件。)
Then convert the avi to a gif:
然后将 avi 转换为 gif:
ffmpeg -i video.avi -pix_fmt rgb24 -loop_output 0 out.gif
You can get windows binaries for ffmpeg here.
您可以在此处获取ffmpeg 的 Windows 二进制文件。
You can also do a similar thing with mplayer. See Encoding from multiple input image files.
你也可以用mplayer做类似的事情。请参阅从多个输入图像文件编码。
I think the command line would be something like:
我认为命令行将类似于:
mplayer mf://*.jpg -mf w=800:h=600:type=jpg -vf scale=160:120 -vo gif89a:fps=3:output=out.gif
(Where 800 & 600 are your source width and height and 160 & 120 are the target width and height.out.gif is your target file name)
(其中 800 和 600 是您的源宽度和高度,160 和 120 是目标宽度,height.out.gif 是您的目标文件名)
I've just tested both of these and they both work fine. However I got much better results from mplayer as I was able to specify the resolution and framerate. Your milage may vary and I'm sure you could find more options for ffmpeg if you looked.
我刚刚测试了这两个,它们都工作正常。但是我从 mplayer 得到了更好的结果,因为我能够指定分辨率和帧率。您的里程可能会有所不同,我相信如果您查看,您可以找到更多 ffmpeg 选项。
回答by psycho brm
With ImageMagick:
使用 ImageMagick:
convert *.png a.gif
回答by Loisaida Sam Sandberg
The ffmpegto .aviand .avito .gifworked, but the only thing to note is that your images must be named in perfect increasing numeric order to work, with no gaps. I cooked up a quick python script to rename all of my images accordingly so that this ffmpegrecipe would work:
该FFMPEG至.AVI和.AVI到.gif注意的工作,但要注意的唯一的事情是,你的图像必须在完善被命名为增加数值为了工作,没有间隙。我编写了一个快速的 python 脚本来相应地重命名我的所有图像,以便这个ffmpeg配方可以工作:
import os
files = [ f for f in os.listdir('.') if os.path.isfile(os.path.join('.',f)) and f.endswith('.jpg') ]
for i, file in enumerate(sorted(files)):
os.rename(file, 'image%03d.jpg' % i)
And then I stumbled upon a much simpler approach than ffmpegfor doing the conversion, which is simply using ImageMagick's command line convert tool like this
然后我偶然发现了一种比ffmpeg更简单的方法来进行转换,它只是使用 ImageMagick 的命令行转换工具这样
convert image%03d.jpg[0-198] animated_gif.gif
Doesn't get much simpler than that folks.
没有比那些人更简单的了。
Gist here: https://gist.github.com/3289840
要点在这里:https: //gist.github.com/3289840
回答by luator
Based on the answers of Simon P Stevens and dwurf I came up with this simplified solution:
根据 Simon P Stevens 和 dwurf 的回答,我想出了这个简化的解决方案:
ffmpeg -f image2 -framerate 1 -i image%d.jpg video.gif
This results in a rate of 1 second per image. Adjust the framerate
value according to your needs.
这导致每个图像的速率为 1 秒。framerate
根据您的需要调整该值。
回答by Alexander Pacha
I'd just like to add to dwurf's answer, that this will generate a gif with the standard 256-colors palette, which does not look very visually pleasing.
我只想添加到dwurf的答案中,这将生成一个带有标准 256 色调色板的 gif,看起来不太美观。
I've found twoblog-postsand adapted them to my needs, in order to improve the visual quality by using a custom palette for your animation:
我找到了两篇博客文章,并根据我的需要对其进行了调整,以便通过为动画使用自定义调色板来提高视觉质量:
Generate the color palette:
生成调色板:
ffmpeg -f image2 -i image%d.jpg -vf scale=900:-1:sws_dither=ed,palettegen palette.png
Convert images into a regular video with the desired framerate, because the third command only worked with a single input video and not a bunch of images
将图像转换为具有所需帧率的常规视频,因为第三个命令仅适用于单个输入视频,而不适用于一堆图像
ffmpeg.exe -f image2 -framerate 1.2 -i image%d.jpg video.flv
Now convert the generated video with the generated palette into a more beautiful gif:
现在使用生成的调色板将生成的视频转换为更漂亮的 gif:
ffmpeg.exe -i video.flv -i palette.png -filter_complex "fps=1.2,scale=900:-1:flags=lanczos[x];[x][1:v]paletteuse" video.gif