ImageMagick montage command -page flag not working - imagemagick

Input Original Image size - 1062x880
Final Requirement - on Full A4 Page Portrait Mode Centered Image Tiled 1x1.
Something like this below.
The code below
montage -page A4 -gravity Center -tile 1x1 image.jpg im.jpg
gives the output like this below.

Related

How to resize overlay image by ratio and with relative position it from the right?

Let's start with a background and an overlay image:
magick convert -size 500x500! xc:red background.jpg # make a big red background
magick convert -size 100x100! xc:blue overlay.jpg # make a smaller blue overlay
To composite from right side I can use
$geom=magick convert overlay.jpg -print "+%[fx:w+50]+0" null:
magick convert background.jpg overlay.jpg -gravity northeast -geometry $geom -composite output.jpg
However, in my real project, I need to run this with various background images, whose sizes are also various. I would like the overlay to use relative size to the background instead of absolute size.
To overlaying a watermark/logo with relative dimentions, I can use:
magick background.jpg overlay.jpg -resize %[fx:t?u.w*0.9:u.w]x%[fx:t?u.h*0.9:u.h] -gravity northease -composite output.jpg
To resize overlay image by ratio and with relative position it from the right, I try:
magick background.jpg overlay.jpg -resize %[fx:t?u.w*0.1:u.w]x%[fx:t?u.h*0.1:u.h] -gravity northeast -geometry +[fx:t?u.w*0.1:u.w]+[fx:t?u.h*0.1:u.h] -composite output.jpg
But it says:
magick.exe: invalid argument for option '-geometry' '+[fx:t?u.w*0.1:u.w]+[fx:t?u.h*0.1:u.h]' at CLI arg 7 # error/operation.c/CLISimpleOperatorImage/2522.
The documentation for geometry doesn't seem to talk about this. Do you know why?
I'm using v7 on Windows
I miss the %. Correct code:
magick .\base.jpg .\logo.png -resize %[fx:t?u.w*0.1:u.w]x%[fx:t?u.h*0.1:u.h] -gravity northeast -geometry +%[fx:t?u.w*0.03:u.w]+%[fx:t?u.w*0.03:u.w] -composite output.png
See Format and Print Image Properties
The reasoning behind the %[fx:t?u.w*0.9:u.w]
From The FX Special Effects Image Operator:
u: first image in list
v: second image in list
t: index of current image (s) in list
w: width of this image
So in plain language, it means that if the image in question is the second image, whose index is one, of which the ternary conditional operator also read as true, then resize it to 90% width of the first image, else do no resize. Or else -resize option will apply to each images in an image sequence (i.e. all input images before it, but not after it).

imagemagick montage - how to align images to bottom

I want to create a montage, using ImageMagick, where all the images are aligned to the bottom. The images have different heights, and -gravity South doesn't behave as I expect.
Image 1 is 100px high.
Image 2 is 200px high.
If I use :
montage *.png -tile 2x1 -background None -geometry +20+0 -gravity South out/montage.png
I get:
If I add a third image, of 50px high, and run
montage *.png -tile 3x1 -geometry +20+0 -gravity South montage2.jpg
I get:
I understand what's happening here - the canvas is expanding from the bottom. Is there any way to get it to grow from the top, so that the image looks like:
You can just use +smush (documentation) for that:
convert -background white -gravity south [abc].png +smush 10 result.png
If using Imagemagick v7, replace convert with magick.

How to convert PNG to pdf and center the image?

I'm using Magick's convert tool to convert and combine all my images into PDF.
magick convert *.png -page a4 out.pdf
The resolution of the images are 1079 x 1397
The images in the generated PDF file are off-centered. There's some white space on the top of each page.
Here's a screenshot : https://i.imgur.com/EpvxZNU.png
I don't want to "fill" the entire page with my image. I want to simply "fit" the image the way it has in the screenshot above, BUT be centered (have equal whitespace on the top and bottom)
I don't believe that you can center with -page in ImageMagick 7 unless you do inline computations using the actual desired A4 size in pixels (592x842) via distort and its viewport setting. See page sizes in pixels at https://imagemagick.org/script/command-line-options.php#page
In ImageMagick 7, you should use magick and not magick convert. The latter will emulate ImageMagick 6. However there is a bug currently in ImageMagick 7. The following command works fine with convert in ImageMagick 6, but fails with magick in ImageMagick 7. But it works fine with magick convert in ImageMagick 7.
magick convert *.jpg -virtual-pixel white -set option:distort:viewport "592x842-%[fx:(592-u[t].w)/2]-%[fx:(842-u[t].h)/2]" -distort SRT 0 +repage result.pdf
Here is a simple example:
Input Images:
magick convert lena.jpg barn.jpg -virtual-pixel white -set option:distort:viewport "592x842-%[fx:(592-u[t].w)/2]-%[fx:(842-u[t].h)/2]" -distort SRT 0 +repage result.pdf
Result:

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: convert image to PDF with A4 page size and image fit to page

I want to convert different image formats (bmp,jpg,gif,png,tiff-incluging multipaged) into a PDF format with A4 page size and with images fit to page (resized if necessary). Image should be positioned at the center of the page and I'd like to define an offset.
I tried the code below but there is no offset at the top and the image quality is really poor.
convert png.png -gravity North -resize 500x500 -quality 100 -page a4x5x5 myout.pdf
Is there any way to do that?
Thanks in advance for any help,
Mariusz
If you want to keep the original resolution (lossless) you can try the following command:
convert png.png -background white -page a4 myoutput.pdf
Based on a comment posted before: https://stackoverflow.com/a/24573341/6747994
#m4tx This command only makes sense if the picture has a resolution above 500x800px, it does not zoom in, to avoid pixelated thumbnails.
You can convert to pdf using ImageMagick
convert png.png myout.pdf
but use pdfjam instead of ImageMagick to adjust the page size
pdfjam --paper a4paper --outfile myoutA4.pdf myout.pdf
pdfjam offers other options, which may fit your needs.
Found this somewhere on stackoverflow:
convert *.jpg -resize 1240x1753 \
-extent 1240x1753 -gravity center \
-units PixelsPerInch -density 150x150 multipage.pdf
Thanks to the ImageMagick support forum I was able to find a solution:
convert image.tif -resize 575x823^> -gravity center -background white -extent 595x842 image.pdf
If you get an error try:
convert image.tif -resize 595x842^\> -gravity center -background white -extent 595x842 image.pdf
You need to specify the density:
convert -density 80 -page a4 input_A.jpg input_B.jpg output.pdf
Otherwise, there can be an issue when printing with "actual size" instead of "fit". I have used a public printer where I could not choose "fit" and had to choose "actual size", in which case the printed page was only a part of the page which I wanted to print.

Resources