overlaying multiple generated layers in one command

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
taywa
Posts: 2
Joined: 2016-05-02T08:23:48-07:00
Authentication code: 1151

overlaying multiple generated layers in one command

Post by taywa »

hi everybody

using imagemagick 6.7.7-10 on an ubuntu 14.04 LTS server i'm trying to create a single image from multiple layers in one command. i can create the layers in seperate commands, but i don't want to save all the layers as seperate files.

td;dr:
how can i write all these steps in a single (convert?) command without creating all the intermediate files:

Code: Select all

# color background:
convert area.jpg -background '#f3cba3' -alpha Shape -background '#558eba' -alpha remove area_color.jpg

# transparent red outline:
convert line.jpg  -negate -background '#CC0000' -alpha Shape line_color.png

# combine them with an offset:
convert area_color.jpg -page +10+10 line_color.png -flatten -crop +10+10 +repage final.jpg
i tried something like this - but it's not working at all (ends up with just a red image):

Code: Select all

convert \
	area.jpg -background '#f3cba3' -alpha Shape -background '#558eba' -alpha remove \
	-page +10+10 \
	line.jpg  -negate -background '#CC0000' -alpha Shape \
	-composite  -crop +10+10 +repage  final.jpg

description with example images:

these are my base images:
area.jpg
Image
line.jpg
Image

these commands generate the layers:
area_color.jpg:

Code: Select all

convert area.jpg -background "#f3cba3" -alpha Shape -background "#558eba" -alpha remove area_color.jpg
Image

line_color.png (with transparent background):

Code: Select all

convert line.jpg  -negate -background '#CC0000' -alpha Shape line_color.png
Image


then i can combine these layers to create the final image:

Code: Select all

convert area_color.jpg -page +10+10 line_color.png -flatten -crop +10+10 +repage final.jpg
Image

since i will be doing this in a loop with a lot of different colors, i need to find a way without writing all these intermediate layers out to files.

how far i got myself
i managed to reduce one step (without the -page and +repage) by creating the 'line_color.png' on the fly:

Code: Select all

convert area.jpg -background "#f3cba3" -alpha Shape -background "#558eba" -alpha remove area_color.jpg
convert \
	line.jpg  -negate -background '#CC0000' -alpha Shape \
	area_color.jpg -reverse -composite final_nocrop.jpg
but i can't integrate the "area_color.jpg" generation into the seccond command. nor do the page/repage correctly.

any hints from the pros?
please?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: overlaying multiple generated layers in one command

Post by snibgo »

These are solid-colour images. Why are you using lossy JPEG compression?

If you write your third convert like this (bash syntax):

Code: Select all

convert \
  area_color.jpg -page +10+10 \
  line_color.png \
  -flatten \
  -crop +10+10 +repage \
  final.jpg
... then, for each input image, substitute the image expressions that make them. Put each input in parentheses.

Code: Select all

convert \
  \( area.jpg \
    -background '#f3cba3' -alpha Shape -background '#558eba' -alpha remove \
    -page +10+10 \) \
  \( line.jpg -negate -background '#CC0000' -alpha Shape \) \
  -flatten \
  -crop +10+10 +repage \
  final2.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: overlaying multiple generated layers in one command

Post by fmw42 »

Use parenthesis processing and clones if needed to keep all the commands properly separated, but in one long command line. See

http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone

Something like this:

Code: Select all

convert \( area.jpg -background '#f3cba3' -alpha Shape -background '#558eba' -alpha remove \) \
\( line.jpg  -negate -background '#CC0000' -alpha Shape \) \
-geometry +10+10 -composite -crop +10+10 +repage final.jpg
But I have never seen the usage of -crop without WxH.

What are you trying to do with the crop?

Using -chop twice seems to do the same thing:

Code: Select all

convert \( area.jpg -background '#f3cba3' -alpha Shape -background '#558eba' -alpha remove \) \
\( line.jpg  -negate -background '#CC0000' -alpha Shape \) \
-geometry +10+10 -composite -gravity north -chop 0x10 -gravity west -chop 10x0 +repage final1.jpg
The documentation for geometry does say:

"Occasionally, [size]offset is possible. In no cases are spaces permitted within the geometry argument."

So I guess that is allowed and just trims the top and left sides of the image.
taywa
Posts: 2
Joined: 2016-05-02T08:23:48-07:00
Authentication code: 1151

Re: overlaying multiple generated layers in one command

Post by taywa »

thank you, snibgo and fmw42 for your replys. they both work as expected. also thank you for the links to the manual. i've seen the

@fmw42 about your questions:
- why jpg instead of gif?
i used jpg's only in the example because they are scaled down from the gif's from the server. the post would have been even longer (visualy) with 1920px heigh images.

- why crop (without WxH)?
i shift the lines 10px southeast so if i want it to go to all over the image i need to chop/crop 10px northwest. i can't use WxH because i don't know the size of the image. the command should work for any sized image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: overlaying multiple generated layers in one command

Post by snibgo »

I fairly often use "-crop" with just one of W and H, and sometimes without either.

If W or H is missing, it defaults to the image width or height.
snibgo's IM pages: im.snibgo.com
Post Reply