Using montage to highlight only one image in array

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
another
Posts: 3
Joined: 2018-08-11T12:42:49-07:00
Authentication code: 1152

Using montage to highlight only one image in array

Post by another » 2018-08-11T12:48:54-07:00

Hello,
First time post. I just recently found ImageMagick and am in love with it. I have a question related to the montage function.
Let's say I have 4 images as one.png two.png three.png four.png
I am building a 2x2 array using this command:

montage one.png two.png three.png four.png -geometry 800x800+20+20 complete.png

My question is how can I highlight or frame or identify just one of the four images? For example, let's say I want to show that
the correct image on this array is two.png. So I want to someone draw the viewer's attention to just this one piece of the array.
In another version of the montage it might be three.png and so on.

In other words, each montage I create will have a different image that I want to identify as the intended image on the array.
Is there a way to do this with montage?
Many thanks in advance!

User avatar
fmw42
Posts: 23825
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using montage to highlight only one image in array

Post by fmw42 » 2018-08-11T13:05:43-07:00

You cannot do that in Montage. But you can put a frame around one image, then use montage to combine them. Or just use append.

see

https://www.imagemagick.org/Usage/crop/#frame
https://www.imagemagick.org/Usage/layers/#append_array

User avatar
GeeMack
Posts: 625
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Using montage to highlight only one image in array

Post by GeeMack » 2018-08-11T14:06:38-07:00

another wrote:
2018-08-11T12:48:54-07:00
My question is how can I highlight or frame or identify just one of the four images? For example, let's say I want to show that the correct image on this array is two.png. So I want to someone draw the viewer's attention to just this one piece of the array. In another version of the montage it might be three.png and so on.

In other words, each montage I create will have a different image that I want to identify as the intended image on the array. Is there a way to do this with montage?
As fmw42 mentioned, "montage" may not be the right tool for this job. Using "convert" you can read in several images, isolate one in parentheses to highlight it in some way, then "+append", "-crop", and "-append" them into a grid of images for the output. Here's a simple command in *nix syntax that puts a red border around a selected image then combines them all into a single montage...

Code: Select all

highlight=3

convert img1.png img2.png img3.png img4.png \
   \( -clone $highlight -shave 5 -bordercolor red -border 5 \) -delete $highlight -insert $highlight \
   -bordercolor white -border 10 +append -crop 2x1@ -append -border 10 output.png
That reads four images into a list, clones one of them inside the parentheses, shaves and puts a border on it, deletes the original from that position in the list, and inserts the modified image into that position. Then it puts a white border around each of them, assembles them into a grid, and puts one more border around that before outputting the result.

Setting the variable "highlight" ahead of the command lets you select the index of the image you want to emphasize. The indexing starts at "0".

User avatar
fmw42
Posts: 23825
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using montage to highlight only one image in array

Post by fmw42 » 2018-08-11T14:44:44-07:00

Please, always provide your IM version and platform when asking questions, since syntax may differ. Also provide your exact command line and if possible your images.

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown

______


In unix syntax, this was what I was getting at to put a frame around img2. I use convert to process your images such that I only frame image2. Then I write all images to miff: format and pipe the results to montage. The - gets all the images from the miff:

Code: Select all

convert img1.png \( img2.png -mattecolor white -frame 10x10+5+5 \) img3.png img4.png miff:- | montage - -tile 2x2 -geometry 800x800+20+20 result.png
or only process the second image, but use montage to read all of them.

Code: Select all

convert img2.png -mattecolor white -frame 10x10+5+5 miff:- | montage img1.png - img3.png img4.png  -tile 2x2 -geometry 800x800+20+20 result.png
If you are not resizing the images from the -geometry 800x800, then it is better to just use -geometry +20+20. I generally resize outside of montage.

another
Posts: 3
Joined: 2018-08-11T12:42:49-07:00
Authentication code: 1152

Re: Using montage to highlight only one image in array

Post by another » 2018-08-11T19:47:00-07:00

Thank you for these responses! This is perfect.

I apologize for not including my IM version. I am using 7.0.7 on a Mac (High Sierra 10.13.6).

Since this process will involve quite a bit of code (creating a clean montage and a new one with the highlighted image), I tried creating a bash script:

Code: Select all

#!/bin/bash
montage one.png two.png three.png four.png -geometry 800x800+20+20 img1.png
convert one.png \( two.png -mattecolor red -frame 10x10+5+5 \) three.png four.png miff:- | montage - -tile 2x2 -geometry 800x800+20+20 img1a.png
When I run it I get an error: magick cannot be opened because of a problem. Check with the developer to make sure magick works with this version of macOS. You may need to reinstall the application. Be sure to install any available updates for the application and macOS.

The code works fine if I run it individually line-by-line... I was just hoping to automate it since it will involve about 200 lines. Any suggestions?

User avatar
fmw42
Posts: 23825
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using montage to highlight only one image in array

Post by fmw42 » 2018-08-11T20:13:01-07:00

For IM 7, you need to change convert to magick and montage to magick montage
#!/bin/bash
montage one.png two.png three.png four.png -geometry 800x800+20+20 img1.png
convert one.png \( two.png -mattecolor red -frame 10x10+5+5 \) three.png four.png miff:- | montage - -tile 2x2 -geometry 800x800+20+20 img1a.png
I do not understand your first montage command. Why are you using that? The result is not used in the second command?

The correct IM 7 command would be simply:

Code: Select all

magick one.png \( two.png -mattecolor red -frame 10x10+5+5 \) three.png four.png miff:- | magick montage - -tile 2x2 -geometry 800x800+20+20 img1a.png
How did you install IM 7 on your Mac?

What do you get from

Code: Select all

magick -version

What do you mean by 200 lines? I only see two lines in your bash script! What exactly are you trying to automate? Please be more specific.

another
Posts: 3
Joined: 2018-08-11T12:42:49-07:00
Authentication code: 1152

Re: Using montage to highlight only one image in array

Post by another » 2018-08-11T20:53:10-07:00

I am running Version: ImageMagick 7.0.8-9 Q16 x86_64 2018-08-04

I installed IM using macports.

I have 400 .png image files. I want to create 100 2x2 arrays of the images. I'm using the montage command for that.

Code: Select all

magick montage one.png two.png three.png four.png -geometry 800x800+20+20 img1.png
magick montage five.png six.png seven.png eight.png -geometry 800x800+20+20 img2.png
These montage images (img1.png to img100.png) are what we will call the "clean images."

Next, I want to create another version of each clean image, which highlights one of the four png files in the array. We will call this the "highlighted image." I was using the convert command for that (which I've edited here to magick).

Code: Select all

magick one.png \( two.png -mattecolor red -frame 10x10+5+5 \) three.png four.png miff:- | magick montage - -tile 2x2 -geometry 800x800+20+20 img1a.png
magick five.png six.png \( seven.png -mattecolor red -frame 10x10+5+5 \) eight.png miff:- | magick montage - -tile 2x2 -geometry 800x800+20+20 img2a.png
This gives me a clean image (img1.png) and a highlighted image (img1a.png).
I want to create 100 clean images and 100 highlighted images. I can do this line-by-line in the terminal but if I try and run it as a bash script (even just 10 lines) it crashes and gives me the error I pasted in my last message. To be clear, I can do everything at this point I need to do. The montages look beautiful and I can successfully highlight the target image. I would just like to automate this task, if possible.

User avatar
fmw42
Posts: 23825
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using montage to highlight only one image in array

Post by fmw42 » 2018-08-11T21:03:16-07:00

How do you call this script:

Code: Select all

#!/bin/bash
magick montage one.png two.png three.png four.png -geometry 800x800+20+20 img1.png
magick one.png \( two.png -mattecolor red -frame 10x10+5+5 \) three.png four.png miff:- | magick montage - -tile 2x2 -geometry 800x800+20+20 img1a.png
What have you named it?

Lets say you call it montage_script. Then you would call it as

Code: Select all

bash montage_script
That should work if all your image and the script are in one folder and you initiate the script from that folder. Otherwise, you need to put the full path to the script and potentially the full paths to your images.

Assuming this works for these 4 images, if you need to do this for multiple sets of four, then you need to write some script code to feed it the images that you need or processed sets of 4 images in a loop. The script code must find all the images in a folder and then process them in groups of 4. If you need to highlight the same image in the command line position, then that is easy. It is harder to loop and tell it which image is to be highlighted. You would need to have for example a file that list the image to be highlighted for each loop iteration.

Post Reply