ComputersLinuxVideo Editing

Batch resizing images on Linux or Windows – ImageMagick convert command

With the resolution of today’s digital cameras it is often necessary to resize the files to make them smaller. You may want to reduce the file size before publishing them on the web, or sending in an email. When you have one or two images then it’s not a problem.

Just load the file into the GIMP photo editor (for Linux or Windows), or Windows Vista users can use Microsoft Paint (although not on Windows XP or earlier).

If however you have a large number of images then this can get a bit tedious manually applying this to each one. There is however the convert command which is part of the ImageMagick suite. If you are running Linux then it may be installed already, just man convert for more information. If not then you can install using sudo apt install imagemagick. Windows users will have to install Cygwin – makes various Linux programs available for Windows. Download the Cygwin Setup Program and tick ImageMagick during the package selection. Once installed you can then run the Cygwin Bash Shell terminal and then enter the Linux commands into that screen.

The most basic way to use convert is to give a file at a time on the command line:

convert -resize 33% original.jpg newfile.jpg

Alternatively you could replace the percentage with the actual size such as 800×600

Where this really comes into it’s own is when you have a directory full of images that you want to convert. This can be used by using the xargs and find commands in conjunction with convert.

find . -iname "*.jpg" | xargs -l -i convert -resize 800x600 {} ../small/{}

Another example to resize images and convert to 8big PNG (256 colours) is using:

find . -name "xmas*.png" | xargs -l -i convert -resize 64x32 -colors 256 {} {}

Resizing Videos

On a related note here is something that I found to be useful for resizing videos. In particular I often run my computer with two screens. When I recorded a video using simplescreenrecorder I forgot to restrict it to a single screen and so ended up with a really large video covering both. The following command was used to crop just the top screen. The variables 0:0 is the origin of the video I want to keep which in my case was my top screen.

ffmpeg -i inputvideo.mp4 -filter:v “crop=1920:1080:0:0” -crf 18 outputvideo.mp4