Create a gif out of multiple png files and retain the original size

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
searene
Posts: 3
Joined: 2017-04-15T01:50:51-07:00
Authentication code: 1151

Create a gif out of multiple png files and retain the original size

Post by searene »

I have multiple png files, all of them are of size 575x500, I created a gif out of those png files using the following command:

Code: Select all

convert -delay 100 -loop 0 *.png a.gif
It worked, except the size of a.gif was 1920x1080, which was the same as my screen resolution. Most areas of the gif is transparent, only an area of size 575x500 in the middle is not, and this area is what I want.

I would like to ask if there's any way to remove those transparent areas? I only want a gif with the size of 575x500.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create a gif out of multiple png files and retain the original size

Post by snibgo »

searene wrote:I have multiple png files, all of them are of size 575x500, ...
Is that true? What is the text output from "identify *.png"?

I suspect your inputs have a larger canvas. If so, it can be removed with "+repage" after the inputs:

Code: Select all

convert -delay 100 -loop 0 *.png +repage a.gif
snibgo's IM pages: im.snibgo.com
searene
Posts: 3
Joined: 2017-04-15T01:50:51-07:00
Authentication code: 1151

Re: Create a gif out of multiple png files and retain the original size

Post by searene »

Hi snibgo,

Thank you for your reply and suggestion.

These png files are originally 1920x1080, I cropped them to 575x500 using this command.

Code: Select all

mogrify -crop 575x500+715+322 *.png
This is the output of identify *.png:

Code: Select all

➜  Desktop identify *.png
2017-04-15-160537_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 173c 3.41KB 0.000u 0:00.000
2017-04-15-160607_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 10.3KB 0.000u 0:00.000
2017-04-15-160618_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 10.3KB 0.000u 0:00.000
2017-04-15-160808_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 14KB 0.000u 0:00.000
2017-04-15-160819_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 13KB 0.000u 0:00.000
2017-04-15-160847_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 16.4KB 0.000u 0:00.000
2017-04-15-160858_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 17.3KB 0.000u 0:00.000
2017-04-15-160913_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 19.3KB 0.000u 0:00.000
2017-04-15-160946_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 20.3KB 0.000u 0:00.000
2017-04-15-161002_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 21.5KB 0.000u 0:00.000
2017-04-15-161012_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 21.1KB 0.000u 0:00.000
2017-04-15-161020_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 19.9KB 0.000u 0:00.000
2017-04-15-161046_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 23KB 0.000u 0:00.000
2017-04-15-161053_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 24.7KB 0.000u 0:00.000
2017-04-15-161119_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 26.1KB 0.000u 0:00.000
2017-04-15-161123_1920x1080_scrot.png PNG 575x500 1920x1080+715+322 8-bit sRGB 256c 12.9KB 0.000u 0:00.000
I guess it meant the size of those png files were all 575x500? And it also pointed out those images were cropped out from images of size 1920x1080, with offset 715 and 322.

I just tried the command you provied, and it worked pretty well!

Code: Select all

convert -delay 100 -loop 0 *.png +repage a.gif
I'm still curious why the original gif file, which was created without +repage, was of size 1920x1080.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create a gif out of multiple png files and retain the original size

Post by snibgo »

searene wrote:I'm still curious why the original gif file, which was created without +repage, was of size 1920x1080.
That's because you didn't have "+repage" when you created the PNG files, so they ended up with a canvas size of 1920x1080. The canvas size doesn't merely mean the image was cropped. It means the image sits on a larger canvas, at a certain offset.

After "-crop", you should always consider putting "+repage", because in many circumstances (such as here) you don't want the canvas data. See http://www.imagemagick.org/script/comma ... s.php#crop
snibgo's IM pages: im.snibgo.com
searene
Posts: 3
Joined: 2017-04-15T01:50:51-07:00
Authentication code: 1151

Re: Create a gif out of multiple png files and retain the original size

Post by searene »

Hi snibgo,

Thanks for the explanation, it's very clear.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Create a gif out of multiple png files and retain the original size

Post by fmw42 »

If the rest of the image is transparent or constant color, you could try

Code: Select all

convert -delay 100 *.png -coalesce -trim +repage -loop 0 -layers optimize a.gif
Post Reply