How to batch convert entire directory of animated gifs

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
ViPeR_930
Posts: 6
Joined: 2015-09-18T13:31:52-07:00
Authentication code: 1151

How to batch convert entire directory of animated gifs

Post by ViPeR_930 »

Hi,
How do I batch resize animated gifs with imagemagick? I have this script which works for single images but I must specify both the exact image name.

convert {path to image with filename}.gif -coalesce -resize 200x200 \
-layers optimize-frame {output name}.gif

mogrify -path seems to batch process but animations all come out broken with mogrify.

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to batch convert entire directory of animated gifs

Post by snibgo »

I'd put it in a loop, so "convert" is called once per gif. When batch-converting a load of images with common elements in the name I always read from one directory and write to a different one.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to batch convert entire directory of animated gifs

Post by fmw42 »

Does the same command using convert mangle your animations? I tried a simple animation using mogrify and it worked fine. I did not do the optimize.

What is your IM version and platform? Can you upload your animation to dropbox.com and put the URL here, so others can test your command?
ViPeR_930
Posts: 6
Joined: 2015-09-18T13:31:52-07:00
Authentication code: 1151

Re: How to batch convert entire directory of animated gifs

Post by ViPeR_930 »

Than you both for the quick replies.

snibgo, can you tell me how to change my command so it runs in a loop and outputs to a different folder?

fmw42, the convert command I have above works fine for the small batch of animated gifs I tried it with. Mogrify botched everything up. I can test more again tonight. I am using imagemagick 6.7 through SSH on my Centos server.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to batch convert entire directory of animated gifs

Post by fmw42 »

IM 6.7 is very old (approx 200 versions old). You would do well if you could upgrade. It could be a bug in such an old version of IM. But if you can provide one of your gif animations as requested above, I can test it in mogrify with 6.9.2.1 Q16 Mac OSX.

#create output directory if it does not exist already

Code: Select all

cd to inputdirectory
list=`ls *.gif`
echo "$list"
for img in $list; do
convert "$img" -coalesce -resize 200x200 \
-layers optimize-frame "path2outputdirectory/$img"
done
ViPeR_930
Posts: 6
Joined: 2015-09-18T13:31:52-07:00
Authentication code: 1151

Re: How to batch convert entire directory of animated gifs

Post by ViPeR_930 »

Thanks for the help. I updated to 6.9 and mogrify now works for batch resizing the gifs.
ViPeR_930
Posts: 6
Joined: 2015-09-18T13:31:52-07:00
Authentication code: 1151

Re: How to batch convert entire directory of animated gifs

Post by ViPeR_930 »

For a script such as this, where can I insert "nohup" command so it runs after I close the ssh connection? I put nohup at the mogrify line but it stops after completing the first directory.

Code: Select all

cd
list=`find /backup/gifs/upload/*/big -type d`
echo "list=$list"
for directory in $list; do
echo "directory=$directory"
cd $directory
nohup  mogrify -path ../medium -monitor -resize '280' *.gif
cd
done
Post Reply