Newbie needs help with "composite"

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
thatseattleguy

Newbie needs help with "composite"

Post by thatseattleguy »

Help. (IM 6.2.8 on RHEL5.)

1) I have large series of files with each member of the series consisting of two PNG images. One image is the background (the form) and ones the foreground (the data fields that fill in the form).
2) They're both monochome, but I have no idea if the background color on each image is white or transparent (how to tell? does it matter?)
3) They're different sizes - the foreground is smaller than the background.
4) All I want to do is composite (overlay) the foreground and background, preserving everything in each image, and stretching the width of the foreground to make it the same as the width of the background (so they overlay evenly). The result should look like a filled-in form.

I'm having real problems with this and think I'm just having conceptual difficulties understanding IM's model. :) I'm playing with lots of variations of "composite" and "convert" but all that happens is that the composite image ends up with both of the old images, side by side, or one image obliterates the other completely. I'm not getting anywhere with actual overlaying of both together, let alone getting the sizes right.

What am I doing wrong here? Most of what I've tried is variations on:

# convert background.png foreground.png -gravity center -flatten newimage.png

thanks for any help and pointers.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newbie needs help with "composite"

Post by anthony »

Summery: you are only dealing with monomcrome images, and the foreground is black, but background is white or transparent.

First -flatten does not understand gravity!!!!
It is not used when 'layering' images.
Second if the overlay image has a white background this will erase any black parts of the background image.


Okay. First sanitise the input so that it is all one type of image

In this case the simplest would be make all backgrounds white!
For multiple images that is best dong using -border! Believe it or not!

Code: Select all

    -bordercolor white -border 0x0
Now you only have black on white images. multiply them
and the images will preserve black. I have use this myself to overlay diagrams onto a page of black text on a white background.

Code: Select all

   -compose multiply  -composite
So for each pair of images do...

Code: Select all

   convert background.png  overlay.png \
                  -bordercolor white -border 0x0 \
                  -gravity center -compose multiply  -composite \
                  result.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
thatseattleguy

Re: Newbie needs help with "composite"

Post by thatseattleguy »

anthony wrote:

Code: Select all

   convert background.png  overlay.png \
                  -bordercolor white -border 0x0 \
                  -gravity center -compose multiply  -composite \
                  result.png
Thanks. I would never have guessed using "bordercolor" this way. Awesome.

But....while that works, but it doesn't solve the mis-matched size issue. Do I somehow need to know the size of the background image and use some kind of '-geometry' setting for the foreground to make it bigger, to match? Or is there a way to just say "stretch the foreground to whatever the background is"? Or am I stuck with making sure the images are identical sizes before I even start?

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

Re: Newbie needs help with "composite"

Post by fmw42 »

thatseattleguy wrote:
anthony wrote:

Code: Select all

   convert background.png  overlay.png \
                  -bordercolor white -border 0x0 \
                  -gravity center -compose multiply  -composite \
                  result.png
Thanks. I would never have guessed using "bordercolor" this way. Awesome.

But....while that works, but it doesn't solve the mis-matched size issue. Do I somehow need to know the size of the background image and use some kind of '-geometry' setting for the foreground to make it bigger, to match? Or is there a way to just say "stretch the foreground to whatever the background is"? Or am I stuck with making sure the images are identical sizes before I even start?

thanks
TSG

Use -geometry widthxheight{+-}x{+-}y rather than -gravity. But you will then need to know at least where you want it placed on the background (which will have to be as large or larger than the foreground/overlay)

See http://www.imagemagick.org/script/comma ... p#geometry

Note that -geometry can resize the last image in the sequence. So you can tell it the size of the background and it will resize the foreground to match.

See http://www.imagemagick.org/Usage/resize/#geometry

Otherwise, if you want the foreground image to match the background, you will need to use -resize either beforehand or in your command line. See -resize at http://www.imagemagick.org/script/comma ... php#resize
thatseattleguy

Re: Newbie needs help with "composite"

Post by thatseattleguy »

Use -geometry widthxheight{+-}x{+-}y rather than -gravity. But you will then need to know at least where you want it placed on the background (which will have to be as large or larger than the foreground/overlay)
Thanks. Will do. For now I'm doing this instead:

resize and convert the PCL form data and call it the background:
# pcl6 -dNOPAUSE -PPCL5C -sDEVICE=pcxmono -sOutputFile=./formdata.pcx -r200 -g1700x2200 formdata.pcl

(note the original form data is coming from a PCL output stream, so I need to convert it to an image format anyways - I'm doing this with the ghostpcl package).


Now composite the background "data" and the foreground "form" together
(note the "foreground" pcx image is about 1600px wide, so smaller than the "background" image):
# convert formdata.pcx forboxes.pcx -bordercolor white -border 0x0 -gravity north -compose multiply -composite form_out.png

This isn't perfect yet, but it's getting close.

thanks /TSG/
~
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newbie needs help with "composite"

Post by anthony »

If you want to place the overlay image relative to either a corner or the center of the background, then you DO NOT need to know the size of the background image. The geometry option defines the offset of the image relative to the gravity setting. If no gravity is defined it offsets from the top left corner.

This is why gravity is so useful with composition.
for example
-geometry +10+10 -gravity SouthEast -composite
will overlay the image 10 pixels from the lower right hand corner! (leaving a 10 pixel gap from the edge).

So in general NO you do not need to know how big the background image is.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply