June 13, 2023
Resize images in Linux

So I build (a lot of) websites. During this process I often receive a folder with a LOT of (large) images. These images sometimes exceed 10MB each, which is ridiculous when it comes to web.

My websites resize all images on build, so the website visitors do not notice these gigantic file sizes. However, during build time these huge files have to be read and processed. This causes a longer (initial) build time, while I always aim for instant deploys. Additionally they increase the repository size dramatically, which is also very inconvenient and inefficient. Therefore, I always resize these images (manually) before I add them to my repository.

Today I decided that doing this manually was a complete waste of my time and I promised my (future) self that I would never open them one by one again (for this purpose). It was time for a cool command line script to do this for me. I took me 10 minutes to find the perfect script, which was less than I expected. I ended up with this:

mogrify -resize '3000x3000>' -quality 50 -format jpg *.*

It loads/uses imagemagick, which can be installed with ‘sudo apt install imagemagick’. Then it overwrites the files (if required), which is called ‘mogrify’ and resizes them to a maximum width and height of 3000 pixels. Finally it outputs them as JPG with 50% image compression. Your output files will typically be somewhere between 250kb and 1MB. Neat, huh? This single command is going to save me a lot of time.

PS. I decided I also wanted to slugify and lowercase the filenames… so here are those commands:

rename 's/ /-/g' *
rename 'y/A-Z/a-z/' *

You are welcome, future Joost!

()  Joost van der Schee

next blog post next post previous blog post previous post Scroll to top