# Create gif from saved images

**URL:** https://discourse.julialang.org/t/create-gif-from-saved-images/85146
**Category:** General Usage
**Tags:** plots, animations
**Created:** [August 2, 2022, 7:44am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146 "2022-08-02T07:44:45Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 2, 2022, 7:44am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/1 "2022-08-02T07:44:45Z")

</div>

I have 3000 images.They are the result of calculations on Julia. This images is a graphic of probablity density function (PDF), time series and lyapunov spectrum. How can i create gif from images PDF, time series and lyapunov spectrum ?  
Images of PDF have name type of “\* PDF” where \* is number. Similarly for time series and Lyapunov spectrum that is “\* timeseries”; “\* spectrum”.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [August 2, 2022, 8:30am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/2 "2022-08-02T08:30:12Z")

</div>

Might be related to [Plotting a sequence of images as a gif](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/)

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 2, 2022, 3:41pm UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/3 "2022-08-02T15:41:10Z")

</div>

it won’t help me. I tried this

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [August 2, 2022, 5:49pm UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/4 "2022-08-02T17:49:44Z")

</div>

With Plots you can create an animation from your png files only if these files have the names “000001.png”, …“003000.png”,  
because the file names for animation frames are predefined here [https://github.com/JuliaPlots/Plots.jl/blob/master/src/animation.jl#L16](https://github.com/JuliaPlots/Plots.jl/blob/master/src/animation.jl#L16)  
If the pngs are already generated and saved in a folder, and you don’t want to repeat the computations that led to your images it would be nice if there was a way to change their names through code. I don’t know whether this is posssible or not.

Once your pngs have the right name, then define the vector of their names, and animate your frames:

```julia
import Printf.@sprintf
import Plots:Animation, buildanimation  
nframes =3000                
fnames = [@sprintf("%06d.png", k) for k in 1:nframes]   
anim = Animation("PDF", fnames); #PDF is the folder name which contains the pngs
buildanimation(anim, "path-to-gif/your-animation-name.gif", fps = 12, show_msg=false) #set a suitable fps

```

But if you can repeat the computations that generated your pngs, then you can create an animation, usually, with Plots,jl.

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 2, 2022, 5:52pm UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/5 "2022-08-02T17:52:35Z")

</div>

Thank you. i try this

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 3, 2022, 8:47am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/6 "2022-08-03T08:47:46Z")

</div>

Hi! I hope you solved your problem with @empet’s excellent advice.

If you find you’re doing this type of thing often, you might find it useful to have FFMPEG.jl in your toolbox. FFMPEG is the “Swiss Army knife” of image and video processing, and it’s conveniently wrapped in a Julia package.

If all your PNG files are named in the same way, you can write code such as this:

```julia
using FFMPEG
imagesdirectory = "/tmp/temp/"
framerate = 30
gifname = "/tmp/output.gif"
FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%10d.png -y $(gifname)`)

```

In this command, the `%10d.png` matches all files named with just 10 digits followed by “.png”. If your first file was `00001-timeseries.png` you would use `%05d-timeseries.png`. The `-y` overwrites the GIF output file if it already exists.

You can use similar commands to create ‘real’ videos - this function:

```julia
FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%10d.png -vf "scale=1920:1080" -c:v libx264 -pix_fmt yuv420p -y "/tmp/output-video.mov"`)

```

rescales every image then outputs a video in a MPEG 4 container file.

(In a test I just did with this bigger output, the GIF file size for 150 frames was 198Mb, the equivalent movie was 4Mb. Presumably a fair amount of lossy compression going on.)

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 3, 2022, 9:58am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/7 "2022-08-03T09:58:15Z")

</div>

Thank you. That helped me. The previous answer also worked, but required extra steps to rename the files

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 7:45am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/8 "2022-08-05T07:45:22Z")

</div>

I have a problem when creating a gif. Path to folder is correct, he is worked for saving files

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d23da7a434dba5bd868b65c465a1943ea176f9d.png)

For example this is worked:

```julia
fig.savefig("res_HR_full_small/full/$i.png")

```

and this code don’t work:

```julia
imagesdirectory = "res_HR_full_small/full/"

framerate = 30
gifname = "res_HR_full_small/full/output.gif"

FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%1000d.png -y $(gifname)`)

```

Also i used cd() for test:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/a/da9c01b195ddfc737786d54314d1a36cf5fba60d.png)

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 5, 2022, 8:06am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/9 "2022-08-05T08:06:05Z")

</div>

The files should be named consistently with regard to the number of digits, `lpad()` is useful:

```julia
for i in 1:10
    println(lpad(i, 10, "0"))
end

```

Then, the _number_ of digits is specified in the command. So `%10d.png` for 10 digit filenames such as `0000000010.png`. (so not %1000d…)

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 8:10am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/10 "2022-08-05T08:10:06Z")

</div>

I need rename all files ?

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 5, 2022, 8:12am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/11 "2022-08-05T08:12:59Z")

</div>

You’ll probably have to do that anyway - at the moment they’re not in order since the last file appears in the list before the second file - unless you sort the list by numerical value.

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 8:18am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/12 "2022-08-05T08:18:07Z")

</div>

Okay, i try  
How fix this line code for correct save file ?

```julia
fig.savefig("res_HR_full_small/full/$i.png")

```

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 5, 2022, 8:21am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/13 "2022-08-05T08:21:54Z")

</div>

Use `lpad()` to make filenames all the same length, so replace `$i` with

```julia
   $(lpad(i, 10, "0"))

```

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 8:34am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/14 "2022-08-05T08:34:39Z")

</div>

Thank you

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 9:02am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/15 "2022-08-05T09:02:43Z")

</div>

I created gif but it has broken images. For example this is images

 ![00717](https://global.discourse-cdn.com/julialang/original/3X/4/2/4230932bc48c94de354be83259603337a31c9db0.png)  
 ![00716](https://global.discourse-cdn.com/julialang/original/3X/d/1/d1e9a7ba97c0f8ddc7709abbd0317edc47245b56.png)  
and this gif  
 ![output](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae635cb3b818cb713bfcf34798447f0990934189.gif)

Output:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/5/250049d5ecce751b6e7abc22f40f015ddc5b3ae5.png)

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 5, 2022, 9:10am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/16 "2022-08-05T09:10:52Z")

</div>

I’m glad you got it working. But now your question moves into a different area - video quality, color palettes, etc.

Assuming all your input files are correct … I can suggest a few things, since video stuff isn’t my thing.

- try making sure there’s a solid background for each image, such as pure white. Animated GIFs don’t always like “transparent” backgrounds.

- try another output format; GIF is OK for smaller purposes, but it looks like other video formats might do better

- ffmpeg lets you work with GIF color palettes. It’s out of scope for this question and my knowledge, but searching the internet may lead you to some useful sources, such as [this](https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/), where the (possibly biased 🙂 ) author writes:

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 9:13am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/17 "2022-08-05T09:13:14Z")

</div>

Okay, thank you

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 9:19am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/18 "2022-08-05T09:19:02Z")

</div>

i try create video and this help. Now i have correct colors in animation  
again thank you

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [August 5, 2022, 9:28am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/19 "2022-08-05T09:28:29Z")

</div>

It would be great to paste your solution here if possible to help others 🙂

---

<div class="post-metadata">

### Author: ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)
#### Post date: [August 5, 2022, 9:33am UTC](https://discourse.julialang.org/t/create-gif-from-saved-images/85146/20 "2022-08-05T09:33:56Z")

</div>

Okay  
That is code for video:

```julia
using FFMPEG
imagesdirectory = pwd()
gifname = "\\output.gif"
gifname = imagesdirectory * gifname

framerate = 10

FFMPEG.ffmpeg_exe(`-framerate $(framerate) -f image2 -i $(imagesdirectory)/%5d.png -vf "scale=1200:900" -c:v libx264 -pix_fmt yuv420p -y "C:\\Users\\olenin.s\\Desktop\\test\\output-video.mov"`)

```

This is code for rename files:

```julia
for i in range(1, 1001)
    mv("$i.png", "$(lpad(i, 5, "0")).png")
end

```

but before run this code need go to directory with files  
commalnd for switch directory:

```julia
cd("directory")

```

also command to know current directory:

```julia
pwd()

```

code for save images ( I using package PyPlot):

```julia
filename = "$(lpad(1, 4, "0")).png"
directory = "C:\\Users\\olenin.s\\Desktop\\res_HR_full_small\\"
directory = directory * filename

fig.savefig(directory)

```
