ImageMagick: Take Folder of PNGs of Variable Size and Convert to a GIF

3/4/2020 9:46:29 PM

ImageMagick is a great command line image processor. The following uses ImageMagick in Powershell and takes a folder of PNGs of variable size, converts them to a standard size, and then creates a final gif file.


################# variables

# directory containing individual images with wildcard match string
$input_folder = "G:\Software Development\Projects\Image Magick\Input Files\*.png"


# full output path to the gif you are creating
$output_file_path = "G:\Software Development\Projects\Image Magick\perfect.gif"


# folder used to store temporary files
$tmp_folder = "G:\Software Development\Projects\Image Magick\Temp\"
$tmp_file = $tmp_folder + "1.png"
$tmp_glob = $tmp_folder + "*.png"


# delay between each gif frame
$gif_delay = 10



#$input_folder
#$output_file_path
#exit


################# process


# delete tmp files
Get-ChildItem -Path $tmp_folder -Include *.* -File -Recurse | foreach { $_.Delete()}


# resize input pngs to 400x400, centered, transparent background
# not all input files are 400x400
magick convert $input_folder -background none -gravity center -extent 400x400 $tmp_file


# create gif
magick convert -loop 0 -delay $gif_delay -dispose Background $tmp_glob $output_file_path



echo "CONVERSION COMPLETE ================================================= "