Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I batch-resize images in ImageMagick while maintaining aspect ratio and a max width and height?

I have a folder of hi-res images. It's a mix of JPG and 24-bit transparent PNG. I want to batch-generate thumbnails with the following rules:

  • each image retains its format (.jpg in, .jpg out... .png in, .png out)
  • each image retains its filename but gets moved to a /thumbnails subfolder
  • each image retains its current aspect ratio
  • the final dimensions of each processed image should be as large as possible with these constraints:
  • max image width is 488px
  • max image height is 220px
  • aside from scaling, each image should retain all of its other properties

I am new to ImageMagick and have no idea what command(s) I would need to run in order to achieve this result. Ultimately, this is for a Jekyll project, but I would happily settle for being able to run this process manually via command line.

Any help would be much appreciated!

like image 285
Aaron Sarnat Avatar asked May 25 '19 13:05

Aaron Sarnat


People also ask

How do I resize an image in ImageMagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new. png.


1 Answers

ImageMagick command line mogrify is likely what you want and can do what you need. It processes a folder full of images. See https://imagemagick.org/Usage/basics/#mogrify and https://imagemagick.org/script/command-line-options.php#resize. The command would like be something like the following:

Create a new directory to hold your output, say, thumbnails.
Change directory to your folder with the images

mogrify -path path_to/thumbnails -resize 488x220 *


That command will process every file in your folder. If you have other formats, such as txt or other images that you do not want to process, then modify the command to restrict to just png and jpg as follows:

mogrify -path path_to/thumbnails -resize 488x220 *.png *.jpg


add *.PNG *.JPG *.jpeg *.JPEG to the end of the line, if you have different caps and spellings for suffixes in your directory.

There are different APIs for other programming languages. See https://imagemagick.org/script/sitemap.php#program-interfaces

like image 54
fmw42 Avatar answered Oct 03 '22 00:10

fmw42