How to specify what tile a image should occupy (montage)

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?".
Mofler
Posts: 10
Joined: 2013-06-11T05:31:27-07:00
Authentication code: 6789

How to specify what tile a image should occupy (montage)

Post by Mofler »

Hi!
I'd like to have an image consisting of nine (3x3) cells/tiles.
I then want to populate these cells with one large image taking up 4 cells, and three smaller images taking up one cell each. (like shown in http://postimg.org/image/3o1mk66ut/ )

I do this with

Code: Select all

convert c:\temp\image.jpg -crop 50x50% part.jpg
convert -rotate 180 part-1.jpg part-1.jpg 
convert -rotate 180 part-2.jpg part-2.jpg 
montage -adjoin image.jpg part-1.jpg  part-2.jpg part-0.jpg -geometry +40+40+40+40 result.jpg
remove-item part*.jpg
but I need to specify that image.jpg should occupy four cells, not one like it does now. How do I do this?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to specify what tile a image should occupy (montage)

Post by GreenKoopa »

We crossed, so my idea posted to the old topic. It follows a different technique so I left it there. It uses convert with -append and -gravity.
viewtopic.php?f=1&t=23573#p99528

Your diagram shows borders and (uneven) spacing. Are they desired, or are they for clarity like the letters?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to specify what tile a image should occupy (montage)

Post by fmw42 »

I am confused by your diagram also
Mofler
Posts: 10
Joined: 2013-06-11T05:31:27-07:00
Authentication code: 6789

Re: How to specify what tile a image should occupy (montage)

Post by Mofler »

Thank you very much GreenKopa. Your code is exactly what I am after.
The spacing in my illustration is not intended, just me being sloppy.

I'd like some spacing between the images though, how would I add this?
My script (script.bat) is now the following:

Code: Select all

convert image.jpg -crop 50x50% part.jpg
convert -rotate 180 part-1.jpg part-1.jpg 
convert -rotate 180 part-2.jpg part-2.jpg 
convert -background white -gravity SouthEast image.jpg part-1.jpg +append ( part-2.jpg part-0.jpg +append ) -append result.jpg

del part*.jpg
Why can't i simply run this as a batch file?
I get the following error:

Code: Select all

c:\Temp>´╗┐convert image.jpg -crop 50x50 part.jpg
'´╗┐convert' is not recognized as an internal or external command,
operable program or batch file.
(I've edited the file in Notepad..)

(Sorry for the confusing illustration by the way..)
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to specify what tile a image should occupy (montage)

Post by GreenKoopa »

That is odd. You must have some non-printable characters or encoding problem. Try retyping it into a new file. It's not an ImageMagick problem. Do remember that % has special meaning in batch files, so escape them to %%.

You could use -border to add borders or margins to images as needed before -append. Alternatively you could create spacer images (-size 1x1 xc:white) and -append them where needed. -append will join all images in one go. There are many options so if those don't work let me know what specifically you need.
Mofler
Posts: 10
Joined: 2013-06-11T05:31:27-07:00
Authentication code: 6789

Re: How to specify what tile a image should occupy (montage)

Post by Mofler »

Thank you very much once again!
Why are you putting the last two pieces in paranthesis?
I'd like to put all of this in a powershell script but it breaks on the paranthesis. I need a way to escape it to be run in PS.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to specify what tile a image should occupy (montage)

Post by GreenKoopa »

I believe your cryptic error is a UTF-BOM.

I wish I knew more about Powershell, but I don't. Parenthesis need to be escaped on several platforms.

I use parenthesis to limit the second +append to the images we want on the second row. Take them out to see what happens.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to specify what tile a image should occupy (montage)

Post by snibgo »

Mofler wrote:'´╗┐convert' is not recognized as an internal or external command,
You have saved your command file in Notepad as UTF-8 encoding. Those funny characters at the start say it is UTF-8. The Windows command interpreter doesn't like UTF-8 files.

Solution: save the file as ANSI encoding.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to specify what tile a image should occupy (montage)

Post by GreenKoopa »

Parenthesis could be avoided, but it would quickly become a mess. There must be some way to escape them. Backslash, backtick, quotes, something.

There is a Windows usage page, but nothing on Powershell. If you become a frequent ImageMagick user, sharing your Powershell tips, tricks, and traps would be appreciated by all.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to specify what tile a image should occupy (montage)

Post by snibgo »

Poweshell escaping:

Code: Select all

PS D:\web\im> convert rose: "(" "+clone" ")" -append r.png
PS D:\web\im> convert rose: `( "+clone" `) -append r.png
PS D:\web\im> convert rose: `( `+clone `) -append r.png
snibgo's IM pages: im.snibgo.com
Mofler
Posts: 10
Joined: 2013-06-11T05:31:27-07:00
Authentication code: 6789

Re: How to specify what tile a image should occupy (montage)

Post by Mofler »

Thank you so much everybody!
I have to say that Imagemagick really stands out in terms of user involvement helping others.
I now have exactly what I need thanks to you.

The reason that I wanted to do this in Powershell and not bat is that I am an sysadmin and Powershell is the future for scripting on Windows so no need to do it any other way.

You have all helped me immensely.

This is my final script:

Code: Select all

convert image.jpg -crop 50x50% part.jpg
convert -rotate 180 part-1.jpg part-1.jpg 
convert -rotate 180 part-2.jpg part-2.jpg 
convert -background white -gravity SouthEast -bordercolor white -border 20 image.jpg part-1.jpg +append "("  -bordercolor white -border 20 part-2.jpg part-0.jpg +append ")" -append result.jpg

Remove-Item part*.jpg
Have a great week:)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to specify what tile a image should occupy (montage)

Post by snibgo »

Good stuff, but I strongly advise against storing intermediate results as JPG, as this loses quality.
snibgo's IM pages: im.snibgo.com
Mofler
Posts: 10
Joined: 2013-06-11T05:31:27-07:00
Authentication code: 6789

Re: How to specify what tile a image should occupy (montage)

Post by Mofler »

Thanks snibgo!
What is the alternative?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How to specify what tile a image should occupy (montage)

Post by GreenKoopa »

Intermediate jpegs work well enough for this command and this input, but can later lead to problems. A lower-quality (higher compression) input image can result in more loss per step. And some commands will amplify the loss. Better off safe unless you have a reason to do otherwise. I use png for intermediate files. Easy and compatible with everything. miff, ImageMagick's format, has advantages. It can store multiple images and so sometimes can be less intuitive for new users. Most efficient is to eliminate intermediate files, but sometimes this can leave code unreadable. ImageMagick is full of alternatives.

Placing operations before images is depreciated, so
convert -rotate 180 part-1.jpg part-1.jpg
should be
convert part-1.jpg -rotate 180 part-1.jpg
or this could be easily moved into the final command as
... "(" part-1.jpg -rotate 180 ")" ...

-bordercolor is a setting, which is persistent until changed. The second one is not needed, but not harmful. For example, -background white and -gravity SouthEast are used for every following append.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to specify what tile a image should occupy (montage)

Post by snibgo »

Mofler wrote:Thanks snibgo!
What is the alternative?
I use png or tiff for development; miff for production. They all support 16 bits/channel and alpha channel, and don't lose any information. png is widely supported by image viewers. miff is faster.

jpg is 8-bit, no alpha, and generally loses information for every write/read.
snibgo's IM pages: im.snibgo.com
Post Reply