I am trying without success to scale an image to 250 px width keeping aspect ratio using ImageMagick and then crop it from top corner (x,y) with size 160 px width and 160 px height.
I have tried this with PHP, but it s not working:
exec($homePath.'/ImageMagick/bin/mogrify -resize 250x -crop 160x160'+'.$x.'+'.$y
.' test.jpg');
I have even tried 2 instructions like below:
//Resize is not working
exec($homePath.'/ImageMagick/bin/mogrify -resize 250x test.jpg');
//Crop is working
exec($homePath.'/ImageMagick/bin/mogrify -crop 160x160'.'+'.$x.'+'.$y.' test.jpg');
I m using ImageMagick 7.0.7-4
Finally with the Help of #fmw42, who focus my attention on the term magick before mogrify in ImageMagick 7:
exec($homePath.'/ImageMagick/bin/magick mogrify -scale 250x
-crop 160x160+'.$x.'+'.$y.' test.jpg');
Thanks.
Related
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
I need to resize an image and the result image should have 300 pixels in height. The image must keep the aspect ratio of the original image.
To specify the height and keep the aspect ratio, use -resize x300
magick convert -resize x300 in.jpg out.jpg
For Windows users:
"C:\Program Files\ImageMagick-7.0.8-Q16\magick.exe" convert -resize x300 in.jpg out.jpg
(change the "7.0.8-Q16" with your own version of ImageMagick)
For more ways to specify the target size, see https://imagemagick.org/script/command-line-processing.php#geometry
I'm trying to crop image by height with this command line:
convert 1053257.png -gravity South -crop 2910x3312+0+0 -background black +repage image-cropped-top.png
The generated image is not cropped correctly, as the dimensions after running the command are 2791 x 3312.
The width is cropped as well!
Can anyone help with this?
The general form is:
convert input.jpg -crop WIDTHxHEIGHT+0+0 result.jpg
If you want to crop to a specific width, say 1024, leaving the height unaffected:
convert image.jpg -crop 1024x+0+0 result.jpg
If you want to crop to a specific height, say 768, leaving the width unaffected - note the height is after the x:
convert image.jpg -crop x768+0+0 result.jpg
If you want to crop to a maximum width and height, say 1024 wide by 768 tall without distorting the aspect ratio:
convert image.jpg -crop 1024x768+0+0 result.jpg
If you want to crop to a specific width and height, say 1024 wide by 768 tall and are happy to allow gross distortions:
convert image.jpg -crop 1024x768+0+0\! result.jpg
Think of the exclamation mark as meaning "just do it!". Note that the backslash is only needed on Linux/Unix/macOS to escape the exclamation mark, you omit the backslash on Windows.
Note, if you are saving the cropped image in PNG format, you probably want to reset the page afterwards so the image "forgets" it used to be part of a larger image:
convert input.jpg -crop 1024x768+0+0 +repage result.png
With ImageMagick a problem like that can occur if you've done a "-trim" to the image before the crop. When you "-trim" an image it can still remember the original page dimensions from before the trim, then when you crop it, it uses those page dimensions as the starting reference for the crop. You probably need to "+repage" before the crop to start with fresh paging information. Try something like this...
convert 1053257.png -gravity South +repage -crop 2910x3312+0+0 +repage image-cropped-top.png
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<' *
I have a canvas.png image which is 2048 x 2048 px.
I have a background.jpg image which is 1920 x 1080 px.
I want to increase the size of the background.jpg and place it on canvas.png, it must fill the whole area but background.jpg should remain in proportion.
When the two images are composed I want to retain the original canvas size of 2048 x 2048 px
If you do not want to modify the aspect ratio, you can try:
convert background.jpg -resize 2048x2048^ -gravity center -extent 2048x2048 fill_background.jpg
It gives:
I think this does what you want but I am using version 7 and it did not wwork properly with the convert in version 7:
magick KhzcT.png zjBGB.jpg -resize x2048 -gravity center -composite output.png
For version 6 change magick to convert
Somehow missed the answer from #xizwi