Linux convert images to gif

How do I create an animated gif from still images (preferably with the command line)?

I want make a .gif animated picture from a given set of .jpg pictures. I would prefer to do it from the command line, so command line tools would be very welcome.

7 Answers 7

You can use ImageMagick package. Install it using the command:

sudo apt-get install imagemagick 

Now you can create a gif from number of pictures( jpg ) using:

convert -delay 20 -loop 0 *.jpg myimage.gif 

ImageMagick users will likely also want: -deconstruct . I have also found that ImageMagick is very memory hungry, see benchmarks: askubuntu.com/a/1102183/52975

To complete @Maythux answer:

To avoid generating a very large file, you can use -resize option:

In my case, I have 4608×3456 images and the generated gif was more than 300M for 32 images

convert -resize 20% -delay 20 -loop 0 *.jpg myimage.gif 
convert -resize 768x576 -delay 20 -loop 0 *.jpg myimage.gif 

Take care of *.jpg

*.jpg sucks a bit when dealing with numeric values, you may generate a gif with unsorted pics.

$ ls|cat 21-33-26_1.jpg 21-33-26_10.jpg //  

As the shots were taken very quickly (10/s) they all have the same modification time and you can't trick using ls -t for example. On ubuntu you can use ls -v instead, something like:

convert -resize 768x576 -delay 20 -loop 0 `ls -v` myimage.gif 

Sorting numerically is quite tricky on Mac OS X though, I guess you'll need to build a custom script.

You can avoid your *.jpg issue by forward padding numbers with zeros. "01.jpg" instead of "1.jpg", and so on. If you get to triple digits, then "001.jpg", "010.jpg", etc.

There are several ways around the filename sequence problem. Including find , sort , brace expansion, and so on. The ls tool is notoriously unsuitable for this kind of thing. Use find . There's a bit of a learning curve, but it's worth it.

If the input has a transparent background, I would also recommend the -dispose Background option. (I realize OP's post is about jpeg images which cannot have transparent backgrounds, but just in case anyone finds this answer via Google, like I did). By the way, regarding the ls sorting, you could always do some bash script hacking inline, e.g.: $(for f in `seq 0 10`; do printf "21-33-26_$f.jpg\n"; done)

ffmeg solution + test data

As of Ubuntu 18.10, ffpmeg 4.0.2-2, ImageMagick 6.9.10-8, I have found that ffmpeg is much faster than ImageMagick, and uses much less memory.

The simplest conversion command is:

ffmpeg \ -framerate 60 \ -pattern_type glob \ -i '*.png' \ -r 15 \ -vf scale=512:-1 \ out.gif \ ; 

You can get my test data with:

wget -O opengl-rotating-triangle.zip https://github.com/cirosantilli/media/blob/master/opengl-rotating-triangle.zip?raw=true unzip opengl-rotating-triangle.zip cd opengl-rotating-triangle 

The important ffmpeg options I want to highlight are:

  • -pattern_type glob : convenient way to select images
  • -framerate 60 : assume 60 FPS on input images, and output the same FPS. ffmpeg cannot know otherwise, since there is no FPS data is in images as there is is in video formats. The 256 input frames take about 4 seconds to finish. -r 15 : optional. Pick one every 4 images so reduce size ( 4 == 60 / 15 ). With it, identify out.gif says that the GIF contains only 64 frames. It still takes 4 seconds to play, so the delay is altered to make things match.
  • -vf scale=512:-1 : optional. Set the width, scale height proportionally, usually to reduce size and save space.

ImageMagick vs ffmpeg benchmark

To get ImageMagick to work, I first had to modify its disk and memory limits at /etc/ImageMagick-6/policy.xml as explained at: https://superuser.com/questions/1178666/imagemagick-convert-quits-after-some-pages

/usr/bin/time -v convert *.png -deconstruct -delay 1.6 out-convert.gif /usr/bin/time -v ffmpeg \ -framerate 60 \ -pattern_type glob \ -i '*.png' \ out-ffmpeg.gif \ ; 

The commands were constructed to produce outputs that are as close as possible to make the comparison valid:

  • /usr/bin/time -v : used to find the maximum memory usage as explained at: https://stackoverflow.com/questions/774556/peak-memory-usage-of-a-linux-unix-process
  • -deconstruct : GIF images can contain just the minimal modified rectangle from the previous frame to make the GIF smaller. ffmpeg calculates those diffs by default, but ImageMagick does not, unless -deconstruct is used. You will basically want to use that option every time with ImageMagick. We can observe the difference with:
out.gif[0] GIF 1024x1024 1024x1024+0+0 8-bit sRGB 256c 16.7865MiB 0.010u 0:00.010 out.gif[1] GIF 516x516 1024x1024+252+257 8-bit sRGB 256c 16.7865MiB 0.010u 0:00.010 out.gif[2] GIF 515x520 1024x1024+248+257 8-bit sRGB 256c 16.7865MiB 0.010u 0:00.010 

The output GIFs have about the same size and look visually identical.

Elapsed (wall clock) time (h:mm:ss or m:ss): 0:56.16 Maximum resident set size (kbytes): 2676856 
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.41 Maximum resident set size (kbytes): 97172 
  • ImageMagick used 2.6Gb of RAM and took about 1 minute
  • ffmpeg used 100Mb of RAM and took 4 seconds

Test hardware: Lenovo ThinkPad P51 laptop, Intel Core i7-7820HQ, 32GB(16+16) DDR4 2400MHz SODIMM, 512GB SSD PCIe TLC OPAL2.

Источник

protrolium / terminal-gif.md

hypervoid force sizing + optimization:
convert -resize '1920x350!' -delay 5 -loop 0 *.png hv.gif
convert -fuzz 3% -resize '1920x350!' -delay 5 -loop 0 -coalesce -layers OptimizeTransparency *.png an.gif

this test yielded impressive reduction:
convert -filter Triangle -define filter:support=2 -thumbnail 1920 -delay 5 -loop 0 -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB *.png output.gif

another version with less color information
convert -filter Triangle -define filter:support=2 -thumbnail 1920 -delay 5 -loop 0 -unsharp 0.25x0.08+8.3+0.045 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -colors 32 -ordered-dither o8x8,8,8,8,4 +map *.png output.gif

convert GIF into movie by extracting frames and recompiling

convert -coalesce some.gif some%05d.png
extract gif frames

ffmpeg -i some%05d.png some.mov
converting image sequence in QuickTime 7 worked more effectively than the above command

rm some*.png
clear extracted frames

convert -coalesce animation.gif target.png

If you want the crop rectangle to start at top corner X: 50 Y: 100 and the crop rectangle to be of size W: 640 H:480, then use the command:
$ mogrify -crop 640x480+50+100 foo.png

To write the cropped image to a new file:
$ convert foo.png -crop 640x480+50+100 out.png

batch conversion:
for i in *.png; do convert "$i" -crop 1920x248+0+400 "$-cropped.png"; done

Источник

How to Make an Animated GIF in Ubuntu

GIF also knows as Graphics Interchange Format; it has become very popular among social media users in absolutely no time since its introduction. Reason behind the popularity is its low size compared to images and videos. GIF posts attracted more users on popular social media platforms like Facebook, Google Plus, WhatsApp, and Twitter than any other image or video posts.

Many of you might have thought, how one can make a GIF on Linux and its distribution like Ubuntu? So, today I’m going to show you just that. We’re going to have a look at several ways to make animated GIF in Ubuntu. So, without making further delay, let’s get started.

Peek is a simple and lightweight screen recorder for Linux and its distros like Ubuntu. It is also optimized for generating animated GIFs. You can just select any area on your screen to create a screencast. Apart from GIF, you can also directly record to WebM or MP4 video file format.

It is a simple animated GIF recorder so it offers limited set of features. But it offers useful features like customizable frame-rate and delay timer.

To install Peek on Ubuntu, run following command in terminal window.

$ sudo add-apt-repository ppa:peek-developers / stable

Byzanz is a part of GNOME project and it is a simple command-line tool to record animated GIFs in Ubuntu. It is a lightweight GIF recording tool that works effortlessly on Ubuntu and other Linux distributions.

The only drawback is that, you have to enter each option manually as it is a command-line tool. It means no drag and select screen area for recording.

Install Byzanz in Ubuntu using this command in Terminal.

3. GIMP
GIMP is a cross-platform image editor available for GNU/Linux, Windows and Mac OS X. It is a free and open-source, raster graphics manipulation and image editing tool. With the help of selection of images from your computer, you can make an animated GIF using GIMP.

GIMP uses layers to make the animated GIFs. You can export or save new GIF to your computer. GIMP is much more than just animated GIF maker, as you can also edit images.

Install GIMP in Ubuntu using following command.

$ sudo add-apt-repository ppa:otto-kesselgulasch / gimp

4. ImageMagick

ImageMagick is another free and open-source cross platform tool for platforms like Linux and its distributions like Ubuntu. You can create, convert, modify and edit raster images using this tool.

You can create an animated GIF using this app but there is a catch; you need to have ready with image frames as you can not do that in this app, you have to use other apps to achieve that.

Once you have done that, you need to export the created frames and then use convert command in following way to create an animated GIF.

-delay: pause button from one image frame to another.
-loop: makes created GIF repetitive.
-dispose: transition pathway from one image frame to next.

To Install ImageMagick, run following command in Ubuntu terminal.

FFmpeg is free and open-source command-line tool for Linux and its distros like Ubuntu. It is cross-platform solution to record, convert and stream audio and video.

FFmpeg can record and convert GIF files from video and audio streams. Even though it is command line tool, you can still configure it according to your needs for desired GIF output. This makes it one of the most feature rich tool for animated GIF making.
To install FFmpeg, run the following command.

So, you can use any of these tools to make an animated GIFs in Ubuntu and other Linux distributions. There are limited number of tools available for GIF making for Linux but these mentioned here standout in terms of overall performance and reliability.

About the author

Swapnil Tirthakar

A Software Engineer who loves football and passionate about traveling. I often spend my free time playing with gadgets and exploring new possibilities in tech world. I am Linux enthusiast and have about 6 years of experience in web development. I have good command on Python, Java, SQL and system security.

Источник

Читайте также:  Раздел linux под windows
Оцените статью
Adblock
detector