Search code examples
bashpnggifimagemagick-convertimage-conversion

Huge whitespace appearing when converting .png to .gif with bash convert


I have a directory called "plots_for_gifs", which contains 105 files, whose names are identical apart from they end in ...000.png, ...001.png ... etc. up to ...104.png. I am trying to convert them to a .gif using:

convert -density 150 -trim -delay 35 -loop 0 ./plots_for_gifs/*.png ./river_diff.gif

The image files are 491x411 pixels, however the gif produced is 7017x4958 pixels! Even though I am including "-trim", and the same occurs even if I add "-size 491x411"... any ideas?

I am running this in a bash shell in Ubuntu 16.04.3.


Solution

  • Mmmmm.... a couple of things.

    You don't need -density at all with PNG files because it only sets the density to be used when rasterising vector files such as SVG. So, you can omit that.

    If, as you say, your images are already the correct size, you don't need -trim. So, you can omit that too.

    You don't need to prefix filenames with ./, as that just means "the current directory" which is the default anyway, so you can omit that.

    Now to the actual problem. I guess your PNG files have been cropped from some larger images and have "remembered" their previous canvas size. The best way to make them forget, is to use +repage after loading them.

    So, without seeing your files, I suspect you want something more like:

    convert -delay 35 -loop 0 plots_for_gifs/*.png +repage river_diff.gif
    

    If you find you do need -trim, add it into the above command before +repage.


    If that doesn't work, please run the following command and paste the output in your original question - by clicking edit underneath it:

    identify plots_for_gifs/*000.png