7

I have a mixed set of images, each one having a slightly different resolution with a slightly different aspect ratio from the other images.

I have tried using commands like:

convert -resize

and:

convert -crop

However I can't seem to figure out the correct command to make all images have a width of 1024 and an aspect ratio of 6x4, without causing the image to stretch or get squashed.

3
  • If you want to change the aspect ratio of an image, you can either stretch/squash it, letterbox it, or crop it. Which are you looking for?
    – cjm
    Apr 26, 2011 at 8:13
  • I would like to crop it. Apr 26, 2011 at 10:11
  • Cross-posted at Unix Stack Exchange. (Don't do that!) Apr 27, 2011 at 19:58

3 Answers 3

4

Note that 1024 is not divisible by 3, so you can't get an exact 6x4 aspect ratio. I'm approximating it to 1024x682.

The command you want is

convert in.jpg -resize '1024x682^' -gravity Center -crop 1024x682+0+0 out.jpg

-resize '1024x682^' scales the image while preserving the aspect ratio so that either:

  • width = 1024 and height >= 682, or
  • height = 682 and width >= 1024

Only one of those criteria can be accomplished without changing the aspect ratio (unless the image was already 6x4).

-gravity Center -crop 1024x682+0+0 cuts a 1024x682 region centered on the center of the image. You could use a different -gravity depending on how you want your images cropped.

You could also include a -filter option to control the algorithm used to resize the image.

2
  • This answer is correct if you want to crop out the area falling outside the aspect ratio - OP implied that was the requirement but didn't actually say so. If you'd rather size the entire image into the given space and use a background fill for the mismatched areas, change 1024x682^ to 1024x682 and replace -crop 1024x682+0+0 with -extent 1024x682 -background 'COLOR' with your chosen color. Jul 8, 2015 at 18:31
  • @ChrisJohnson, in a comment on the question, the OP did say that he wanted to crop rather than letterbox (which is what you're describing).
    – cjm
    Jul 8, 2015 at 18:43
0

I guess you are using Imagemagick.The problem is not related to any software, simply you will need to do a resize keeping each photo ratio, then a crop using the other ratio(you need to loose part of the image, then, and in other cases, add a chunk at the bottom of solid black, etc). I tend to do this with certain freeware, Irfanview. Using Batch option (just open one image in the folder with irfanview, and hit b key) , I'd set advanced options as checked, then use the options there to resize and crop, it offers quite flexibility.

Nothing you couldn't do with Imagemagick, though, Surely you are letting it stretch, but you could possibly force the crop without changing the ratio...

Other way is using Photoshop automate menu, combining with an Action(macro) previously done by you.

0

Original image: 798x591

Target image:

$ convert /var/tmp/eid0.png -crop 798x450-10+50 /var/tmp/eid.png

798 = width  (keep original)
450 = height (customize original to total height)

-10 = x horizontally
+50 = y vertically

it works nicely.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .