Resizing GIFs

Posted on October 31st 2020 in Resources by Pim Debaere.

Resizing GIFs

There are many tools – online and offline – to resize a GIF, some more user-friendly than the other. But what if you want to shrink a whole collection of GIFs? Then a fancy tool is suddenly not so useful anymore.

gifsicle

Fortunately, there is gifsicle: a command-line tool for creating and editing GIFs. And while their website may look like it was straight out of the '90s, the tool does what it promises. In this article I will only discuss the functionality to change the format, but feel free to check the manual to see all the possibilities.

1
.\gifsicle.exe --resize-fit-width 250 -i .\input.gif -o .\output.gif
Fig. 1 – PowerShell script to resize a GIF.

The above is one line of PowerShell to scale a GIF while keeping the ratio. The width becomes 250 pixels, the height is then determined automatically. In this line I assume that both gifsicle and the input file are in the same folder and the script is also executed from there.

Now let's expand the PowerShell script a bit so we can tackle all the GIFs from a given folder. And why not ensure that we can enter the length of the longest side – width or height?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$maxSize = Read-Host -Prompt "New maximum length of the GIFs"

Get-ChildItem ".\original" -Filter "*.gif" | ForEach-Object {
    $filePath = $_.FullName
    $fileName = $_.Name
    $folder = $_.Directory.FullName
    $img = [Drawing.Image]::FromFile($_.FullName)
    
    if ($img.Size.Width -gt $img.Size.Height) {
        .\gifsicle.exe 
          --resize-fit-width $maxSize
          -i $filePath
          -o "${folder}\..\resized\${fileName}"
    } else {
        .\gifsicle.exe 
          --resize-fit-height $maxSize 
          -i $filePath 
          -o "${folder}\..\resized\${fileName}"
    }   
}
Fig. 2 – PowerShell script to resize multiple GIFs.

Now just release this script on your folder with memes and reaction GIFs