Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to batch convert images to a specific megapixel with Imagemagick?

Tags:

imagemagick

I'm sorting my photos and want to resize them to something reasonable. Now the input files are a wild mix of small and big images, some landscape, some tower.

I want to resize them all (above a threshold) to a specific megapixel size, but all descriptions I found so far only resize to either a fixed size / aspect ratio (e.g. 1024x786) or a fixed percentage (e.g. 50%). None of these seem to apply with a folder that has some small images (e.g. 300x400) and images mixed in the range of 3-12 megapixel.

I'm looking for an aspect ratio independent option that puts them all in the same megapixel ballpark. Any suggestions?

like image 925
Sebastian Bartos Avatar asked Oct 30 '25 00:10

Sebastian Bartos


1 Answers

Add \> at the end of your resize to only down-res larger images and not affect smaller ones and maintain aspect ratio too.

convert -resize 1200X800\> image.jpg 

Or if you want a specific number of pixels, regardless of whether landscape or portrait, you can do

convert -resize 1000000@\> image.jpg

so you get a million pixels max, which may be 400x2,500 or 2,500x400 for example.

EDITED

I should maybe point out that the backslash in front of the greater than symbol is a shell escape to stop the shell deciding to redirect output like it normally does when it sees a greater than symbol. You could equally enclose the resize specification in single quotes to prevent the shell seeing it. If that doesn't make sense, then the following two commands are identical:

convert -resize 1000000@\>  image.jpg
convert -resize '1000000@>' image.jpg
like image 163
Mark Setchell Avatar answered Oct 31 '25 20:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!