ImageMagick change an image ratio without cropping? - imagemagick

magick "E:\image.jpg" -extent 1:1.5 "E:\image_x.jpg"
magick "E:\image.jpg" -gravity center -extent 1:1.5 "E:\image_x.jpg"
These codes crop the images. How can I change an image ratio without cropping? I need this ratio w:1 / h:1,5 and without cutting.Thanks..

Related

how to imagemagick crop only width

im trying to crop an image of size 1920x125058 into 900x125058 from the center
so I used magick convert in.png -crop 900x+510+0 out.png but this outputs a file of size 900x28800. shouldn't the height be untouched if it is not mentioned?
In Imagemagick 7, use magick, not magick convert. For center cropping, use -gravity center
magick in.png -gravity center -crop 900x125058+0+0 +repage out.png

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

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

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.

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