How to resize an image in imagemagick but keep an aspect ratio constant - imagemagick

I am trying to resize an image (using imagemagick) to keep it's current aspect ratio but fit it into a 4/3 container.
This is the command I have so far:
magick convert ./horse.jpeg -background white -gravity center -extent 4/3 ./hourse_output.jpeg
This is what I'd like: . As you can see, the image is "put into" a 4/3 container.

My error. The aspect ratios such as 4:3 in ImageMagick -extent will only crop and not pad.
See my bash unix script "aspectpad" at http://www.fmwconcepts.com/imagemagick/index.html, which does what you want I think.
Nevertheless, here is a partial solution for how to do it. But this only works for landscape mode input. Also only with ImageMagick 7 due to the use of inline arguments for -extent. You would have to modify it for portrait mode.
Input (aspect 2:1 = 2/1 = 2):
magick barn_2to1.jpg -set option:wd "%[fx:(4/3)>(w/h)?(4/3*h):w]" -set option:ht "%[fx:(4/3)>(w/h)?h:(w/(4/3))]" -gravity center -background black -extent "%[wd]x%[ht]" result.jpg
Output (aspect 4:3 = 4/3 = 1.33):
Note, that I used background of black so that it was visible here. Change to any other color you want.
If the input landscape aspect is larger than 4:3 (4/3), it will pad on top/bottom. If the input landscape aspect is smaller than 4:3, it will pad on left/right.
Input (aspect=1:1 = 1/1 = 1):
magick lena.jpg -set option:wd "%[fx:(4/3)>(w/h)?(4/3*h):w]" -set option:ht "%[fx:(4/3)>(w/h)?h:(w/(4/3))]" -gravity center -background black -extent "%[wd]x%[ht]" result2.jpg

Use 4:3 not 4/3. But you have not specified any -resize. In ImageMagick 7, use magick only, not magick convert and not convert. For other tools, use magick identify, magick mogrify, etc. But not for convert. See imagemagick.org/script/command-line-processing.php#geometry for the 4:3 issue.

Here is one other way to do it in ImageMagick, if you know the picture is in landscape mode and the image w/h aspect is larger then 4/3. Just pad the top and bottom with plenty of room and then use -extent 4:3 to crop it. This way no computations are needed, so it should work in ImageMagick 6 or 7. If ImageMagick 6, change magick to convert. (If the w/h is less than 4/3 landscape, then pad the left and right.)
Input:
magick barn_2to1.jpg -gravity center -bordercolor black -border 0x100 -background black -extent 4:3 result3.jpg

Related

Imagemagick how to apply gravity and extent to only short images

I have a web page where I list images that should be 600px by 600px (width & height). So I resize all big images to 600x600 using imagemagick, which works fine.
However, some images, when I resize them (in order to keep their aspect ratio), get resized to a size smaller than 600px in height (for example 600x450). So I tried to fill the needed height by using imagemagick options (gravity, background, and extent) like so :
image.combine_options do |img|
img.crop('1200x1200+0+0')
img.resize '600x600'
img.background 'white'
img.extent '600x600'
img.gravity 'center'
end
That gave me what I want for these kind of images (short images). However, this effect of centering is applied to the other images (bigger ones) as well ! Is there any way to prevent that ?
Update:
Just to show you what I mean, I made up this example... if I don't add extent and gravity the image will start from the top left corner
but if I add extent and gravity (center) the image will start from the center, something like this :
I want only images that result on resized images smaller than 600x600 to be centered, but in the example as you see even an image that get resized to exact 600x600 get centered !! that's not what I want
the solution :
I end up using shell command using system function in ruby
system "convert #{image.path} -crop 1024x1024+0+0 -resize 600x600 -background white -gravity center -extent 600x600 output.png"
and that worked fine! I don't know why minimagick wasn't working correctly, but I just get rid of that gem from my gemfile which is fine too.
Here is your command in command line Imagemagick with proper resetting of the virtual canvas after the crop.
convert image -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result
Here are two results with slightly different resize arguments. It looks like your commands are using the equivalent of the ^ flag set on the resize argument.
Input:
Proper result without the ^ flag: (Note padded with white)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result1.png
Result with the ^ flag: (Note cropped)
convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600^ -background white -gravity center -extent 600x600 result2.png
The above is Unix syntax. For Windows double the ^ to ^^, since ^ is an escape character in Windows.
Perhaps your issue is with minimagick and not Imagemagick. You can check by testing my command line.

Convert image from 4500x5400 to 4050x4200 keeping aspect ratio

I have an image of 4500x5400, I want to resize it for the height 4200 keeping the width ratio, however the width of the image needs to be 4050, leaving the sides transparent.
I have this ImageMagick command:
convert file.png -resize 4500x5400 -gravity center -background transparent -extent 4050x4200 out.png
However it's cutting the top and the bottom, while it needs to appear.
Do you guys have any idea of how I can make it work?
Appreciate your time!
Try this...
convert input.png -resize 4050x4200 -background none -gravity center -extent 4050x4200 output.png
The "-resize" fits your input image within a container of that size. The "-extent" makes sure the total canvas is that size. The "-background" and "-gravity" make sure extra space is filled with transparent and that the input image is located in the center of the output canvas.

ImageMagick: resize with borders in batch

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.
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<' *

Use ImageMagick to resize and then crop an image from command line

I have a jpg image which I want to resize as follows:
Keeping the aspect-ratio of the image same, resize it to as close as 640x360 as possible, without keeping any portion of the new image "blank"/"empty". That is, it's okay if after resizing, it becomes 800x360 or 640x420, but not okay if it is 400x360 (because 400 is lesser than 640) or 640x200 (because 200 is lesser than 360).
After this, I need to center-crop the above-resized image to the ratio of 16:9.
What would be the easiest set of shell commands which can be used to achieve this using imagemagick?
I tried both
convert 'orig_image.jpg' -resize 640x360 -gravity Center -crop 640x360+0+0 'changed_image.jpg' and
convert 'orig_image.jpg' -resize 640x360 -gravity Center -crop 640x360+0+0 +repage 'changed_image.jpg'
but none worked. -resize 640x360 is resizing the image, but not fulfilling my above requirement. That is, 2448x3264 is resizing to 270x360 instead of 640x480
Perhaps you can use this:
convert orig.jpg \
-resize 640x360^ \
-gravity Center \
-extent 640x360
changed.jpg
Where -resize 64x360^ preserves the minimum aspect ratio, and -extent 640x360 (without +0+0) respected -gravity Center

Use ImageMagick to place an image inside a larger canvas

Getting started with ImageMagic and trying to find a way to do this... If an image is less than 50 pixels tall or 50 pixels wide, I'd like to place it (un-scaled) in the horizontal/vertical center of a new 50x50 pixel canvas on top of a white background - and save that as the new image. Anyone know if this is possible with ImageMagick? Thanks!
I used -extent to do this:
convert input.jpg -gravity center -background white -extent 50x50 output.jpg
I wanted to do the same, except shrink the image to 70% inside. I used this:
convert input.png -resize 70%x70% -gravity center -background transparent -extent 72x72 output.png
Not exactly what was requested but hopefully it will help someone ;).
I have once used this code to place an image in the center of a new canvas with white background. hope this will help you
convert -background white -gravity center your_image.jpg -extent 50x50 new_image.jpg
See cutting and bordering for a huge number of examples. Here's one simple way you might do it:
convert input.png -bordercolor Black -border 5x5 output.png
Of course, you'll need to calculate the size of the border to add (if any) based on the dimensions of the input image. Are you using an ImageMagick API, or just the command line tools?
I tried this:
convert test.jpg -resize 100x100 -background black -gravity center -extent 100x100 output.png
You can use single composition to do this. So it would look something like this:
convert -size 50x50 xc:white null: ( my_image.png -coalesce ) -gravity Center -layers Composite -layers Optimize output.png
To modify the source image you need to use mogrify:
mogrify -gravity center -background white -extent 50x50 source.jpg
If an image is less than 50 pixels tall or 50 pixels wide
In my case, the images were much larger than the destination canvas, and weren't square. So I resize them proportionally to fit inside. Example:
convert in.png -resize 46x46 -background none -gravity center -extent 50x50 out.png
The 46x46 limit ensures a 2 pixel margin minimum. Note that the above does not distort the image, e.g. a rectangle does not become a square.
I used background none for a transparent background, but you can choose a solid color instead.

Resources