Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize images that are larger than X in Ubuntu

In my Ubuntu server, I have a specific directory that contains a large number of images that I wish to resize to a width of 2000px if they are larger than 2000 pixels whilst maintaining their aspect ratio but if an image's width is less than 2000px it shall remain unchanged.

I want to edit the original image and not make a copy and I have no GUI installed on my server.

like image 439
Adam Scot Avatar asked Feb 06 '23 13:02

Adam Scot


1 Answers

You might want to use ImageMagick. It isn't included in the default installations of Ubuntu and many other Linux distributions, so you will have to install it first. Use the following command:

sudo apt-get install imagemagick

You can specify a width (or height) and ImageMagick will resize the image for you while preserving the aspect ratio.

The following command will resize an image to a width of 2000:

convert example.png -resize 2000 example.png

There is also an option so that it will only shrink images to fit into the given size. It won't enlarge images that are smaller. This is the '>' resize option. Think of it only applying the resize to images 'greater than' the given size, the syntax might be a little counter-intuitive.

convert example.png -resize 2000\>  example.png

You can use bash to apply the command to all of your images,

for file in *.png; do convert $file -resize 2000\> $file; done
like image 92
Filip Allberg Avatar answered Feb 09 '23 04:02

Filip Allberg