Merge Images Side by Side (Horizontally) - imagemagick

I have five images of sizes: 600x30, 600x30, 600x30, 600x30, 810x30. Their names are: 0.png, 1.png, 2.png, 3.png, 4.png, respectively.
How do I merge them Horizontally to make an image of size 3210x30 with ImageMagick?

ImageMagick ships with the montage utility. Montage will append each image side-by-side allowing you to adjust spacing between each image (-geometry), and the general layout (-tile).
montage [0-4].png -tile 5x1 -geometry +0+0 out.png
Other examples can be found on Montage Usage page

ImageMagick has command line tool named 'convert' to merge images horizontally, or for other purpose. i have tried this command and working perfectly on your case:
To join images horizontally:
convert +append *.png out.png
To stack images vertically:
convert -append *.png out.png

Use -resize if the images don't have the same width/height
You can fix the height for all of them with the -resize option, e.g. to fix a 500 pixel height on two images joined horizontally:
convert +append image_1.png image_2.png -resize x500 new_image_conbined.png
Or for vertical joins, you would want to set a fixed width instead:
convert -append image_1.png image_2.png -resize 500x new_image_conbined.png
Example:
image_1.png 1067x600
image_2.png 1920x1080
new_image_conbined.png 889x500
Related:
https://askubuntu.com/questions/226054/how-do-i-join-two-images-together
https://superuser.com/questions/290656/combine-multiple-images-using-imagemagick
How to do it interactively with GIMP
If you need to crop/resize images interactively first, which is often the case, then GIMP is the perfect tool for it, here's a detailed step-by-step: https://graphicdesign.stackexchange.com/questions/83446/gimp-how-to-combine-two-images-side-by-side/145543#145543
SVGs
ImageMagick 6.9.11-60 doesn't handle them, so see:
How to concatenate SVG files lengthwise from linux command line?
https://graphicdesign.stackexchange.com/questions/137096/is-there-a-way-to-stack-two-svgs-on-top-of-each-other
https://graphicdesign.stackexchange.com/questions/90844/joining-together-multiple-svg-images

Very simple with ImageMagick (brew install imagemagick )
convert +append image_1.png image_2.png new_image_conbined.png

Anyone using the MiniMagick rails gem can use the built-in tool to merge images:
# Replace this with the path to the images you want to combine
images = [
"image1.jpg",
"image2.jpg"
]
processed_image = MiniMagick::Tool::Montage.new do |image|
image.geometry "x700+0+0"
image.tile "#{images.size}x1"
images.each {|i| image << i}
image << "output.jpg"
end
Check out the documentation for #geometry options to handle resizing and placement. The current example will resize images to a 700px height while maintaining the image's aspect ratio. +0+0 will place the image with no gaps between them.

The convert +append method described in other answers appends images horizontally, aligned to the top. If you prefer to align to the bottom or center, try:
convert input1.png input2.png -gravity South +append output.png
or
convert input1.png input2.png -gravity Center +append output.png
Source: Fred's ImageMagick Tidbits http://www.fmwconcepts.com/imagemagick/tidbits/image.php#append

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 how to -append output split in half

I'm newbie, any help is appreciated. I'll keep it short.
What I'm doing now is "magick *.jpg -append a.png".
This merges 100+ of .jpg vertically into a .png. Problem is its long so I wanna split it in half so I get shorter a.png and b.png instead of 1 long .png.
I would like to have a single code that does it because there is hundreds of these folders.
You could use montage for that in ImageMagick.
magick montage *.jpg -tile 2x -geometry +0+0 result.png
If you want space between files, then change +0+0 to use non-zero amounts for the spacing. Montage can also add file names under the images if you want using -label %f in the command

Black line between 2 images while using append on imagemagick

I have two files;
1.jpg = 14000x2800 pixels
2.jpg = 2800x128 pixels
I use the following command:
...\convert.exe -auto-orient -quality 100 -append "1.jpg" "2.jpg" "out.jpg"
The goal is to merge the images together on the vertical axis, and it works great but now I need to add a straight horizontal black line between images (line's length should be the biggest file's length, meaning from left to right) while appending.
Is there any way to do that without a second command?
Easier description with MSPaint skills
Here's a way to do it:
magick 1.jpg 2.jpg -size "%[fx:u.w>v.w?u.w:v.w]x10" xc:black -swap 2,1 -append result.png
That says... load 1.jpg and 2.jpg. Define the width for the canvas we create next as "whatever is the wider of 1.jpg (referred to as u) and 2.jpg (referred to as v)" by 10 pixels tall. Create a black canvas that size. Now swap the order so that the canvas we just created is between the two images. Append all 3 images and save.
That gives you one of these, depending which one you load first:
If you want the unfilled, white background area beside the narrower image to be, say magenta, use:
magick -background magenta 1.jpg 2.jpg -size "%[fx:u.w>v.w?u.w:v.w]x10" xc:black -swap 2,1 -append result.png
Keywords: ImageMagick, image processing, wider, widest, taller, tallest, match existing image width, match height.

crop / swap parts of image with magick mogrify

I am trying to crop and swap different parts of a big 800x800 image and re-create 800x800 image using imagemagick with this command.
magick mogrify titli.gif -crop 2x4# +repage -reverse -append -path converted titli.gif
my problem is "-append" creates tall image (400x1600) & "+append" creates wide image (3200x200)
How can I get a large image of original size 800x800 but with cropped and swapped (reversed) parts set in "mosaic or tiled" style...
If I understand the question, you shouldn't need "mogrify" to do that. Just "magick" should accomplish that task.
It looks like you'll have to crop the image into 8 pieces, reverse them, and "-append" them vertically as you've done.
Then after that, and in the same command, you'll need to crop that result in half vertically and "+append" those two pieces horizontally to get the 800x800 output.
This example command shows how it works...
magick in.png -crop 2x4# -reverse -append -crop 1x2# +append out.png
If you're doing any more operations within the same command you'll probably want to use "+repage" after the "+append" to reset the image geometry back to WxH+0+0.

How to improve image output from montage in ImageMagick?

I joined images using montage, but the resolution of the image output is less than that of the image's input.
My image's input have dimensions of 640x480 each
But the output that I get was 256x378
I was searching in the web and couldn't find a solution to improve the output's image quality.
The montage command that I'm using
montage -tile 2x3 1.png 2.png 3.png 4.png 5.png 6.png -resize 1024x1024 montage_png.png
Anyone know how can I get better output resolution?
Set the mode of operation style (-mode) to the concatenate value (as in the documentation).
For example:
montage -mode concatenate file1.jpg file2.jpg output.jpg
Suggestion 1
Try it this way... let montage organise the images into a montage and then pass the result on to convert to do the resizing of the result.
montage -tile 2x3 1.png 2.png 3.png 4.png 5.png 6.png miff:- | convert miff:- -resize 1024x1024 montage.png
The intermediate image is passed as a MIFF (Magick Image File Format) which preserves all detail and metadata and quality.
Suggestion 2
If it is always just 5 or 6 images and not hundreds, you can also do it all in one go with convert like this. All you need to know is that +append joins images in a row and -append joins images in a column. So I am joining 1&2 in a row, 3&4 in a row, 5 & 6 in a row and then putting the three rows in a stack and resizing the result.
convert [12].png +append \( [34].png +append \) \( [56].png +append \) -append -resize 1024x1024 result.png
Montage is simple to use like so:
montage -mode concatenate -tile 2x3 *.png -resize 1024x1024 outfile.png
But if you run it a second time this command will also include outfile.png as part of the new montage, of course.
Montage is smart enough to change formats properly, so the command can be changed to:
montage -mode concatenate -tile 2x3 *.png -resize 1024x1024 outfile.jpg
Which results in a file that is 2048 pixels wide.
If the input files are they are all of uniform size, the simplest way is to use it like so:
montage -mode concatenate -tile 2x3 *.jpg outfile.jpg
To the point of the question:
In gnuplot with the png terminal the default canvas size is 640x480, so a 2x3 montage to a proper JPG results in a file that is 1280 pixels wide.
Displayed on a web page that is too wide to print. Print boundaries are roughly 640 pixels wide (depending on how the margins are set), so it will usually work well with:
montage -mode concatenate -tile 2x3 -resize 320x240 *.png outfile.jpg
Using 320x240 preserves the aspect ratio of the original plot.
Forcing it to be square should be done in the set terminal command inside gnuplot if that is the desired outcome.
Best practice for image post-processing is to avoid distorting the image beyond the intent of the program which generates the originals.

Resources