imagemagick montage - how to align images to bottom - imagemagick

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.

Related

Add top and left borders with magick command

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?
Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

imagemagick montage: insert images of different sizes

I have an image a.png of size 800x600, and two images b.png and c.png of size 400x300 each. I want to montage (insert) the three images without size changes into a png image of size 800x900, such that a.png is at the top, and b.png and c.png side-by-side underneath.
How can I do this with imagemagick convert or montage?
Generate sample images, 1/10th the real size:
magick -size 80x60 -background red -gravity center label:"A" a.png
magick -size 40x30 -background lime -gravity center label:"B" b.png
magick -size 40x30 -background blue -gravity center label:"C" c.png
Now, you can load A, and inside parentheses, load B and C and place side-by-side as a new image, then append that result below A:
magick a.png \( b.png c.png +append \) -append result.png
Or, if you dislike the parentheses, you can join B and C side-by-side into a single image, load A, swap the order so A is at the top and then append the combined image below:
magick b.png c.png +append a.png +swap -append result.png
Note the distinction between:
+append meaning "append to the right", and
-append meaning "append below".
Note that, in addition to +append and -append, there are the newer tools called +smush and -smush which do exactly the same but take a parameter which is the number of pixels to offset the appending. So if you do +smush 5 it will do the same as +append but leave 5 pixels of background colour showing in the new gap between the images. If you use a negative offset, it will append in the same place but overlap the two images by the offset.
Here's an example, I make the background magenta and smush B and C with a 10 pixel gap. Then change the background to yellow before smushing the result below A with a 15 pixel offset.
magick -background magenta b.png c.png +smush 10 a.png +swap -background yellow -smush 15 result.png
If still using v6 ImageMagick, replace magick with convert.

How to create an image border?

I want to create something like a border around an image with ImageMagick. I want the border to be related to the original size of the image.
Ex:
A 5% border on a 1000x100px image should give me an image of 1050x105px
A 10% border on a 500x400px image should give me an image of 550x440px
So for a 5% white border I got this code after lots of trial and error. But it seems way over complicated:
convert infile.png \
null: \
\( -clone 0 -resize 105% -fill \#fff -colorize 100% \) \
-swap 0,2 -geometry +0+0 -gravity center -layers composite \
outfile.png
How could I simplify this? What am I missing?
NOTE: I do not want to specify static width of the border since I have multiple input images of multiple sizes.
With ImageMagick you can specify the size of a border as a percent of the width and height. Run a command like this on a 500x400 image to see how it works...
convert input.png -border 5x10% result.png
That should produce an output image with the dimensions 550x480. Keep in mind the percentage you specify is added to each edge, so a 5% border will make a 500 pixel wide image 550 pixels wide.
To add a total of 10% to both the width and height you would use a command like this...
convert input.png -border 5% result.png
You could use -extent like this:
convert -size 1000x100 xc:blue -gravity center -background red -extent 105%x105% result.png
Check
identify result.png
result.png PNG 1050x105 1050x105+0+0 8-bit sRGB 2c 350B 0.000u 0:00.000
Use any of your own images in place of -size 1000x100 xc:blue

Cleaning the left side of an image

Using imagemagick, I want to clean the left side of an image, i.e. make white without cropping. For example cleaning the left-most vertical strip of 25 pixels wide. I figured out how to crop to a given geometry, but I couldn't figure out how to clean without cropping.
Here is my start image, made like this:
convert -size 256x256 gradient:cyan-yellow image.png
Method 1
One way to do it would be to use -fx and set all pixels where the x-coordinate is less than 25 to 1.0 (i.e. white) and leave all other pixels as they are:
convert image.png -fx "i<25?1:u" result.png
Method 2
Another, faster way to do it might be to clone the original image, and scale it down to 25 pixels wide, fill it with white and composite that over the original image:
convert image.png \
\( +clone -scale 25x! -fill white -colorize 100 \) \
-composite result.png
The result is the same.
Method 3
A third way to do it might be to crop the image 25 pixels in from the left side, then splice 25 white pixels back on the left side:
convert image.png -crop +25+0 -background white -gravity west -splice 25x result.png
Method 4
Bit of a kludge, but nearer to what you asked. Here, I guess that your image height doesn't exceed 10,000 pixels and draw a rectangle:
convert image.png -fill white -draw "rectangle 0,0 24,9999" result.png
I guess the proper way to do this is to get the height first then use it:
#!/bin/bash
h=$(convert image.png -format "%[fx:h-1]" info:)
convert image.png -fill white -draw "rectangle 0,0 24,$h" result.png

how do i place a 4 * 6 image on a letter page at the top

I am using imagemagick to convert files and reposition them, i have a 4 * 6 png which i need to position on a letter canvas on the top half of the page.
I have the below command which i am using, but its confusing. can anyone suggest how i can achieve what i want.
this is what i have tried, can any one guide me on this.
convert -rotate -270 -page Letter me-9370120111400937899958.png on-9370120111400937899958.pdf
I have also tried this, but the overlayed image is not moving and is stuck to the bottom
%x{convert -page Letter -rotate -270 "/var/folders/rp/rk2q4l7j4ds_w37vwvgx46tr0000gn/T/a8.png" -geometry +50+50 "/var/folders/rp/rk2q4l7j4ds_w37vwvgx46tr0000gn/T/a8.pdf"}
I have tried reading on this link http://www.imagemagick.org/script/command-line-processing.php#geometry but could not figure out.
Updated Answer
It occurred to me later that you may have meant this:
convert -page letter -gravity north \( a.jpg -background yellow -splice 0x10 \) a.pdf
Obviously change yellow to none and increase the 0x10 to 0x20 to move further down the page, and add -rotate 90 before the splice maybe.
Original Answer
Not sure exactly what you mean, but I think this will get you started. Let's try some options. I will make the canvas yellow so you can see it and the file you want to position on top will be red.
So let's try some options...
First, let's move the image across to the right by zero pixels and down from the top-left by 40 pixels - the default position (or gravity) is NorthWest so we are positioning relative to that.
convert -size 612x792 xc:yellow a.jpg -geometry +0+40 -composite result.jpg
If you want the image centred, use -gravity north and position relative to that - a little closer to the edge this time maybe:
convert -size 612x792 xc:yellow -gravity north a.jpg -geometry +0+10 -composite result.jpg
If you want the background rotated:
convert -size 792x612 xc:yellow -gravity north a.jpg -geometry +0+40 -composite result.jpg
If you want just the overlay rotated, do that in "aside processing":
convert -size 612x792 xc:yellow -gravity north \( a.jpg -rotate 90 \) -geometry +0+40 -composite result.jpg
If you want the canvas white, change yellow to white. If you want the canvas transparent, change yellow to none.

Resources