ImageMagick command to enlarge original image - imagemagick

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

Related

convert doesn't conserve size when scaling down and up

In order to make applying blur faster I'm first scaling my image down and then scale it back up:
convert - -scale 10% -blur 0x2.5 -resize 1000% RGB:-
This works most of the time but sometimes the output resolution is slightly different from the original input. Is there a way to force the pipeline to be size-preserving?
You should be able to access the original geometry of the image via %G, so you can do:
convert input.jpg -scale 10% -blur 0x2.5 -resize '%G!' RGB:-
If you are using Windows, you probably want "%G!" in double rather than single quotes.
If you are using v7 ImageMagick, replace convert with magick.
I think you are getting errors because if you take 10% of 72 pixels (say), you will get a whole number of pixels, i.e. 7 pixels and then when you scale back up by a factor of 10 you'll get 70 rather than your initial 72.
If you are using Imagemagick 7, you can do the following:
magick input.jpg -set option:wd "%w" -set option:ht "%h" -scale "%[fx:wd/10]x%[fx:ht/10]" -blur 0x2.5 -resize "%[fx:wd]x%[fx:ht]\!" RGB:-
This stores the input width and height. Then uses the stored values to scale by 1/10 of those dimensions, then does the blur, then resizes exactly back to the origin input size. Note the ! that forces the resize to the exact dimensions.
or simpler without storing the input width and height:
magick lena.jpg -scale "%[fx:w/10]x%[fx:h/10]" -blur 0x2.5 -resize "%[fx:w]x%[fx:h]\!" lena_x.jpg

Imagemagick: How to bulk convert images into square

I want the size of the square should be equal to the largest side of the original photo
E.g.
Original: 500 x 400 => Output:500 x 500
Original: 400 x 600 => Output: 600 x 600
You can do that as follows for any given image in Imagemagick 7. To do multiple image, you would need to write a "for" loop, whose syntax depends upon your OS.
magick image.suffix -gravity center -background black -extent "%wx%h^" result.suffix
or
magick image.suffix -gravity center -background black -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" result.suffix
Here is a better way to convert all images in a folder with either Imagemagick 6 or 7. I use -distort SRT's viewport to do the padding with a no-op warping (i.e. no rotation, scale or translation) in mogrify to process all images in the folder.
First create a new folder to hold your output images, if desired and use the -path option to mogrify. Specify a background color (in this case I use red).
For Imagemagick 7:
magick mogrify -path path_to/new_folder -virtual-pixel background -background red -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:0.5*(max(w,h)-w)]-%[fx:0.5*(max(w,h)-h)]" +distort srt 0 *
For Imagemagick 6, remove "magick" before mogrify.

crop image with imagemagick offset given in percentage

imagemagick's crop command supports cropping to a percentage of an image but the offset values must be specified in pixel values, e.g.:
convert image.png -crop 50%x+10+20
I want to crop with offset values x and y given in percentage of the image width, and height respectively. The pixel values can be calculated, for instance if the image size is 100x200 an offset of 10% would result in 10 and 20 respectively. Is it possible to do this calculation as part of the call to convert? Width and height are available as %w and %h at some places, but this does not work:
convert image.png -crop 50%x+(0.1*%w)+(0.1*%h)
If you're running IM v6 you can use FX expressions with "-set" to set image attributes. By setting the page geometry you can specify the offsets to a calculated percentage and do the crop like this...
convert image.png -set page -%[fx:w*0.1]-%[fx:h*0.1] -crop 50%x+0+0 result.png
That reads the image, sets the geometry for the upper left corner to a location outside the original canvas, and crops to the new top left corner specified by the geometry.
Note the offsets are negative numbers.
Also, if you're doing additional processing in the same command you'll probably want to "+repage" after the crop in order to reset the page geometry to the new WxH+0+0.
Edited to add: You can even include the width and height dimensions for the crop when using "-set page". This command would crop an output of 50% the input width and height, and starting at 10% in from the left and top...
convert image.png \
-set page %[fx:w*0.5]x%[fx:h*0.5]-%[fx:w*0.1]-%[fx:h*0.1] -crop +0+0 result.png
Notice how the crop operation is simply "-crop +0+0" since the dimensions and offsets are in the page geometry.
This method lets you use more complex calculations than just using a percent or number of pixels for the cropped output dimensions.
You cannot do that in ImageMagick 6. But you can do that in ImageMagick 7.
magick image.png -crop "50%x+%[fx:0.1*w]+%[fx:0.1*h]" +repage result.png
In ImageMagick 6, you need to do the computations ahead of the command, store them in a variable and use the variable in the crop command.
However, in ImageMagick 6, you can do the equivalent using -distort with viewport processing as follows:
convert image.png -set option:distort:viewport "%[fx:0.5*w]x%[fx:0.5*h]+%[fx:0.1*w]+%[fx:0.1*h]" -filter point -distort SRT 0 result.png
With v7 ImageMagick, make start image:
magick -size 200x100 gradient: a.jpg
Now crop using lots of calculated widths, heights, offsets:
magick a.jpg -crop "%[fx:w*0.9]x%[fx:h*0.8]+%[fx:w*0.1]+%[fx:h*0.05]" b.png
Check:
identify b.png
b.png PNG 180x80 200x100+20+5 8-bit Gray 256c 408B 0.000u 0:00.000
If you only have v6, use bash and integer arithmetic:
read w h < <(identify -format "%w %h" a.jpg)
convert a.jpg -crop $((w*80/100))x$((h*90/100))+$((w*10/100))+$((h*5/100)) result.png
Check:
identify result.png
result.png PNG 160x90 200x100+20+5 8-bit Gray 256c 412B 0.000u 0:00.000

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

ImageMagick scale and crop keeping aspect ratio

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.

Resources