2

I have a few images I want to convert in batch using ImageMagick.

All images should be converted to a specific width, lets say 500px.

  • If an image's width is larger than 500px, scale it down and preserve the aspect ratio.
  • If an image's width is less than 500px, add borders to the sides to make it 500px wide.

The first one is possible using magick mogrify -resize 500x *, but this also scales images with a smaller width up. How can I prevent that from happening and add borders instead?

I figured I could just add -background black -gravity center but I don't know how to prevent upscaling.

3
  • 1
    Make a COPY of a few files and try using -resize 500x^> Apr 15, 2018 at 19:18
  • Great! That worked when I added -extent 500x too. So my final command was magick mogrify -resize '500x^>' -background black -gravity center -extent 500x *. If you add your comment as an answer I'll accept it! Apr 15, 2018 at 19:25
  • Oh... This crops images which are more than 500px wide, which is not what I want Apr 15, 2018 at 19:37

1 Answer 1

2

Solved it using two separate commands for now:

Resize images larger than 500px wide:

magick mogrify -resize '500x>' *

Add borders to images smaller than 500px wide:

magick mogrify -gravity center -extent '735x<' *

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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