You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ImageMagick批量调整图片至指定宽度:缩放或添加侧边边框

Solution for Your ImageMagick Batch Resizing & Border Requirement

Got it, let's sort this out—your original magick mogrify -resize 500x * command scales up narrow images because ImageMagick's default resize behavior allows both shrinking and enlarging. Here's how to tweak it to only shrink larger images and add side borders to smaller ones instead:

The Core Command (Directly Modifies Original Files)

Use this if you're confident and have backups of your original images:

magick mogrify -resize "500x>" -gravity center -background white -extent 500x *.jpg

(Replace *.jpg with your target image format, like *.png or * for all images)

Let's Break Down Each Parameter

  • -resize "500x>": The > suffix is key here—it tells ImageMagick to only resize images that are wider than 500px, scaling them down proportionally while keeping their aspect ratio. Narrower images stay at their original size.
  • -gravity center: Ensures your original image stays centered when we add borders, so the extra width is split evenly between left and right sides.
  • -background white: Sets the color of the border. Feel free to change this to any color you want—use black, a hex code like #e5e5e5, or even transparent with none (for PNGs).
  • -extent 500x: Expands the image canvas to exactly 500px wide. The height will automatically match the original image's proportional height, so no stretching or cropping happens.

Safer Alternative (Preserve Original Files)

If you don't want to overwrite your original images, use a loop with convert instead to generate new resized files:

for img in *.jpg; do
  magick convert "$img" -resize "500x>" -gravity center -background white -extent 500x "resized_$img"
done

This will create new files named resized_yourimage.jpg next to your originals, leaving the unmodified files intact.

Example Outcomes

  • If your image is 600px wide: It gets scaled down to 500px wide, height adjusts proportionally, no borders added.
  • If your image is 400px wide: It stays 400px wide, with 50px of border added to both left and right sides, making the total width 500px.

内容的提问来源于stack exchange,提问作者Louis Matthijssen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:00:29