How to fit multiple images in a page optimally using ImageMagick - image-processing

Suppose I have a bunch of images in a folder with different sizes. The goal is to fit images in a number of pages (e.g. A4) in a way that the whitespace is minimal. There shouldn't be any compression or resizing involved. It is acceptable that some images would be rotated.
Here is what I came up with but doesn't try to "fit" images in any way:
montage *.jpg -mode concatenate -tile 2x2 -page A4 -geometry +20+20 out.pdf
Is it possible using imagemagick and montage switches?
I guess the computational geometry algorithm should change the 2x2 and +20+20 part of the command above, right?

In ImageMagick 7 (7.0.10-23 or higher), there is a new feature for doing collages called Ashlar. But it resizes to fit all the provided images into the space allocated.
See https://imagemagick.org/script/formats.php#pseudo
magick *.jpg -define ashlar:best-fit=true ashlar:canvas.png[1000x1000+5+5]
The [1000x1000+5+5] specifies the output dimensions and the minimum spacing in x and y between images.
Here is A4 size result:
magick *.jpg -define ashlar:best-fit=true ashlar:canvas2.jpg[595x842++0+0]

Related

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.

Merge Images Side by Side (Horizontally)

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

ImageMagick: montaging image from different-sized tiles

I'm developing a script to download images from tile-based image-hosting.
I downloaded tiles using wget and trying to use montage to compine them.
The problem is that I've got tiles with different sizes (last tile in row is more narrow than the others). Here is combine command:
montage $temp/*.jpg -tile $maxcolumn"x"$maxrow -geometry -1-1 -quality 100% merged.jpg
ImageMagick aligns tiles by grid and produces this image (see right and bottom sides).
image http://leftparagraphs.ru/!/merged.jpg
How do I fix this with montage?
Fixed by specifying "-mode Concatenate".
Also I have to run another instance of convert after montage to "-trim" resulting image.
This solution did not work for me. To combine two different height images into one, I first used the identify command to get the height of the largest image (1280 pixels):
identify large.jpg
Then I used the following command to resize the smaller image and combine it side by side with the larger one:
montage -tile 2x1 -geometry +0+0 small.jpg"[x1280]" large.jpg output.jpg

How to insert one row spacing between tiles in a tiled image using Imagemagick?

I have a bunch of images that I want to tile together in one row. This can be done using Imagemagick montage like this
montage `ls tile*.png` -tile x1 -gravity west -geometry 1x1\<+0+0 out_file.png
However, now I want to insert one pixel of spacing after each tile. I played with -tile-offset -1+0 for a bit but this would not change anything. Similarly, I could use -geometry 1x1\<+1+0, but this would introduce two pixels of spacing instead of one.
How can I do one pixel of spacing between tiles?
If the images are a known size then you can simply do 1 pixel larger in the -geometry switch. So if the images are 256x256 then use -geometry 257x256

Resources