Compose: Overlay over multiple coordinates w/o loop [Solved]

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
coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Compose: Overlay over multiple coordinates w/o loop [Solved]

Post by coloring » 2018-12-31T11:00:25-07:00

How can I overlay Small.png over Large.png at multiple coordinates without a loop?

I am currently looping this command (+0+0 is replaced with different coordinates every iteration):

Code: Select all

convert.exe Large.png Small.png -geometry +0+0 -compose over -composite Large.png
Could I "-geometry" accept a multi-line variable like the one below?

Code: Select all

+0+0
+11+22
+33+44
+44+0
+0+99
Last edited by coloring on 2019-01-07T07:10:27-07:00, edited 1 time in total.

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

Re: Compose: Overlay over multiple coordinates w/o loop

Post by fmw42 » 2018-12-31T11:30:10-07:00

How about this:

Code: Select all

convert.exe Large.png Small.png ^
( -clone 1 -set page +0+0 ) ^
( -clone 1 -set page +11+22 ) ^
( -clone 1 -set page +33+44 ) ^
( -clone 1 -set page +44+0 ) ^
( -clone 1 -set page +0+99 ) ^
-delete 1 ^
-flatten Large.png
or

Code: Select all

convert.exe Large.png ( Small.png -write mpr:img +delete ) ^
( mpr:img -set page +0+0 ) ^
( mpr:img -set page +11+22 ) ^
( mpr:img -set page +33+44 ) ^
( mpr:img -set page +44+0 ) ^
( mpr:img -set page +0+99 ) ^
-flatten Large.png



see https://imagemagick.org/Usage/layers/

There are various ways to do multiple image composites, but there is nothing like what you desire for -geometry

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2018-12-31T11:57:23-07:00

Thanks! What what if the number of coordinates is not fixed? Can I provide a text file with a list of coordinates? The Pins in a Map example is close, but I am running plain Windows.

If not, perhaps I could use a loop to generate the command text, and then execute the result. The idea is to avoid executing IM more than once.

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

Re: Compose: Overlay over multiple coordinates w/o loop

Post by fmw42 » 2018-12-31T12:19:54-07:00

As far as I know, then only way is to write a script loop that will read your file and loop over doing a -composite or -flatten.

If the number of composites is not too many (that you exceed your OS limit of characters in a line), you can write a loop to create the single command. I do that often in my unix bash scripting.

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2018-12-31T13:45:36-07:00

In Windows, with the maximum length of the command string being limited to 8191 characters and "( -clone 1 -set page +NNNN+NNNN ) " taking up 34 characters -- assuming no shorter forms exist --, the number of coordinates that can fit in a single line is at least 238.

Code: Select all

(8191-90)/34=238

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

Re: Compose: Overlay over multiple coordinates w/o loop

Post by fmw42 » 2018-12-31T13:50:29-07:00

So is 238 acceptable?

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2018-12-31T14:04:20-07:00

80% of the time, yes. I will add a loop to execute IM for every group of 238 coordinates to overcome this limitation. Does that mean there are no shortcuts for -clone and -set page? How come spaces inside the brackets are necessary?

snibgo
Posts: 11922
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose: Overlay over multiple coordinates w/o loop

Post by snibgo » 2018-12-31T14:25:37-07:00

If your command is too long for the shell, write it to a file without the executable name or shell escapes, and run it as a magick script:

Code: Select all

magick -script myscript.txt
snibgo's IM pages: im.snibgo.com

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

Re: Compose: Overlay over multiple coordinates w/o loop

Post by fmw42 » 2018-12-31T15:36:32-07:00

How come spaces inside the brackets are necessary?
That is just the way Imagemagick must be parsed.

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2019-01-02T13:11:01-07:00

snibgo wrote:
2018-12-31T14:25:37-07:00
If your command is too long for the shell, write it to a file without the executable name or shell escapes, and run it as a magick script:

Code: Select all

magick -script myscript.txt
A godsend! Aren't compare.exe, composite.exe, conjure.exe, convert.exe, identify.exe, magick.exe, mogrify.exe, montage.exe, and stream.exe the exact same file, though?

snibgo
Posts: 11922
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose: Overlay over multiple coordinates w/o loop

Post by snibgo » 2019-01-02T13:28:22-07:00

I don't know what you mean. As far as I know, "-script" works with v7 magick, and no other IM program.
snibgo's IM pages: im.snibgo.com

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

Re: Compose: Overlay over multiple coordinates w/o loop

Post by fmw42 » 2019-01-02T13:30:27-07:00

Aren't compare.exe, composite.exe, conjure.exe, convert.exe, identify.exe, magick.exe, mogrify.exe, montage.exe, and stream.exe the exact same file, though?
No each is different. In IM 7, they must be called by prefacing the names with magick, except for convert, which is simply replaced by magick.

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2019-01-02T13:46:01-07:00

Okay. It just seemed odd that they are all the same size (18,945 KB).

coloring
Posts: 74
Joined: 2015-08-27T10:17:36-07:00
Authentication code: 1151

Re: Compose: Overlay over multiple coordinates w/o loop

Post by coloring » 2019-01-02T14:10:30-07:00

Something is wrong. Here is my version:

Code: Select all

C:> magick.exe --version
Version: ImageMagick 7.0.8-16 Q16 x64 2018-12-10 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr
 lzma openexr pangocairo png ps raw rsvg tiff webp xml zlib
Content of myscript.txt:

Code: Select all

img\Large.png img\Small.png ( -clone 1 -set page +0+0 ) -delete 1 -flatten img\Output.png
I am calling myscript.txt in the command line (cmd.exe):

Code: Select all

C:>magick.exe -script myscript.txt
Result:

Code: Select all

magick.exe: UnableToOpenBlob 'imgLarge.png': No such file or directory @ error/blob.c/OpenBlob/3490.
Apparently, it does not like the backslash character (\) for some reason.

I replaced the backslashes with normal slashes in myscript.txt:

Code: Select all

img/Large.png img/Small.png ( -clone 1 -set page +0+0 ) -delete 1 -flatten img/Output.png
After calling magick.exe as before, this is the result:

Code: Select all

magick.exe: UnableToOpenBlob 'img/Output.png': No such file or directory @ error
/blob.c/OpenBlob/3490.
Does the output file have to exist for some reason? I tried creating such a file, but IM just fails silently in that case. The lines below behave as expected when executed directly in the command line:

Code: Select all

convert.exe img\Large.png img\Small.png ( -clone 1 -set page +0+0 ) -delete 1 -flatten img\Output.png
convert.exe img/Large.png img/Small.png ( -clone 1 -set page +0+0 ) -delete 1 -flatten img/Output.png
EDIT: According to https://stackoverflow.com/questions/50876200/, I needed to add -write to fix the Output issue:

Code: Select all

img/Large.png img/Small.png ( -clone 1 -set page +0+0 ) -delete 1 -flatten -write img/Output.png
The backslashes are still a problem, though.

snibgo
Posts: 11922
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compose: Overlay over multiple coordinates w/o loop

Post by snibgo » 2019-01-02T14:20:47-07:00

In Windows, use forward-slashes, or simply cd to the directory first.

For the output, use "-write", eg "-write img/Output.png".
snibgo's IM pages: im.snibgo.com

Post Reply