0

Is there a simple one liner in Terminal that will:

  1. Convert all png files in a folder to jpg files preserving base filenames.
  2. Resize all these files to a specified width while preserving the aspect ratio.

This kind of works but the original filenames are not preserved and I can't tell by the syntax of the command that the aspect ratio is getting preserved:

Convert -resize 1200x900 *.png *.jpg

Ideally I would like something like this to work:

Convert -resize -width 1200 *.png *.jpg

I have read more than ten answers on ask ubuntu but all involve writing loops, scripts, lots of variable syntax, etc. It just feels like there's got to be something simple to at least get the filenames to come out right.

I can live with 1200x900 honoring width and preserving the aspect ratio, but what about when I need the height preserved, which I actually do need sometimes instead.

Thank you!

Update: The marked answer preserves the filenames in a very simple way, which solves my main problem. The first comment leads to how to preserve aspect ratio while specifying width or height desired. All together, I now have the following which solve everything I wanted.

This preserves width:

mogrify -resize 1200x -format jpg *.png

This preserves height:

mogrify -resize x800 -format jpg *.png

The marked answer also added the -quality flag which is really nice:

mogrify -resize 1200x -format jpg -quality 75 *.png

As a bonus, I just love that mogrify overwrites previous output, so I don't end up with a bunch of extra files to delete if I change my mind about size or quality.

Thank you all very much!

2

1 Answer 1

3

Only one command without script? Not with convert but with mogrify (also a part of ImageMagick):

mogrify -resize 1200x900 -format jpg *.png

Note:

  • mogrify check the horizontality or verticality of each image and preserve the aspect ratio.
  • see man mogrify for more options like:
    • -quality 100 to preserve the image quality
    • -path /another/path to put new images in another path (it must be exist before call)

WARNING: If you do not change the output format (vs. the input format) and not choose a different output directory, mogrify OVERWRITES the current image files

1
  • Thank you! I appended my comment to my question. Jul 9, 2022 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.