compsite: using full canvas

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?".
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: compsite: using full canvas

Post by anthony »

only the layers operators expand the size of resulting image.

-flatten expands the first image to cover the virtual canvas setting of only that image.
typically however that is the same size as the actual image so generally no expansion occurs.

For example...

Code: Select all

   convert -page 100x100+20+20 rose: -flatten show:
generates a 100x100 image
It will generate a 100x100 image regardless of where the actual image is located.
If the image is not on the 100x100 pixel canvas it is clipped.

Code: Select all

   convert -page 100x100+70+70 rose: -flatten show:
-mosaic starts with the canvas setting of the first image, but then for each image (including the first again) uses the maximum of that starting size and and the right/bottom edges of the given images on the virtual canvas.

As such this is 100x100 plus the size of the first image as it is also offset by +100+100

Code: Select all

   convert -page 100x100+100+100 rose: -mosaic show:
-layers merge should ALL canvas information, and creates a new image with a virtual offset such that ALL images are visible at their virtual offsets. The canvas size of the first image is simply preserved without modification.

Code: Select all

    convert -page 1000x1000+20+20 rose: -page +70+70 rose: -layers merge show:
    convert -page 1000x1000+20+20 rose: -page +70+70 rose: -layers merge info:
    rose: ROSE 120x96 1000x1000+20+20 8-bit DirectClass 0.120u 0:00.120
Another layers method -layersTrimBounds is closely similar to -layers merge, but does not compose images, but finds the maximum 'bounds' needed to whole all the images, then adjusts the offset so they are all minimally positive.

Code: Select all

    convert -page 1000x1000+20+20 rose: -page +70+70 rose: -layers TrimBounds info:
    rose:[0] ROSE 70x46 120x96+0+0 8-bit DirectClass 9.67KB 0.000u 0:00.029
    rose:[1] ROSE 70x46 120x96+50+50 8-bit DirectClass 9.67KB 0.000u 0:00.029
Note that the smallest offset +20+20 was subtracted from all the images, and the canvas size set to a size that would hold all the image. That is it reset the collective 'bounds' of all the given layer images without actually merging those images. It is especially useful for shrinking 'animations' that does not use all of the specified canvas, or trimmed images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: compsite: using full canvas

Post by anthony »

-set is used to set general settings, and per-image setting (properities)
However if the setting that -set is modifying starts with "option:" then it sets a global artifact (normally set using -define)

[quote=IM Examples Basics, raw unformatted notes...]
Set can be used to set a artifact, instead of Define

-define {setting}={value}
-set option:{setting} {value}

Both will create the artifact {setting} with the given {value}
(watch the equal '=' sign)

The only real difference is tha -set will substitute percent escapes found in {value}, while -define completely ignores any percent escapes in {value}

This difference in percent escape handling is VERY important, and a key component of IM attribute handling.[/quote]

As such if you do not need percent escapes -define is smaller and 'prefered' but -set 'option:...' can be used if you do want to make use of percent escapes.

Most of IM examples now use -define in preference to -set unless percent escapes are needed (some special distortion examples).

I have alls the information about in in IM examples, but it still needs final formatting, and appropriate examples added.
Set and Per-Image Properties
http://www.imagemagick.org/Usage/basics/#set
Define and Global Artifacts
http://www.imagemagick.org/Usage/basics/#define

All the information I wanted is present, just not all formatted and exampled.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Re: composite: using full canvas

Post by dognose »

Wow, this is definitely more complicated than I thought.

Sorry I didn't give more specific examples.. This is a general problem for me, in that I have a number operations and different image types where I need to expand the canvas size, and I was certainly hoping there was an easier way to do it, with out rewriting everything..

This one works pretty well:
convert -background none -page -64-64 zelda3.png -page +0+0 lena2.png -compose blend \
-define compose:args=50,50 -layers merge -alpha off tmp3.png

However, it doesn't give me the same results as the composite command. Rewriting the whole command that way has had undesirable side effects. For example, when the overlay image has some alpha transparency in it, it turns black instead of showing through to the image underneath.

I saw the gradient example with creating the mask before hand, but that is way more complicated in other operations, like when with a diagonal offset. For one of the operations, I was using a faded edge [ -alpha set -virtual-pixel transparent -channel A -blur 0x10 -level 50%,100% +channel ]
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compsite: using full canvas

Post by fmw42 »

Have you tried leaving off the -alpha off at the end:

convert -background none -page -64-64 zelda3.png -page +0+0 lena2.png -compose blend \
-define compose:args=50,50 -layers merge tmp3.png

Otherwise, I guess you have to break the problem down to separate cases (overlapping part and non-overlapping parts), process them as needed and then append them back together as you need.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: composite: using full canvas

Post by anthony »

dognose wrote:This one works pretty well:
convert -background none -page -64-64 zelda3.png -page +0+0 lena2.png -compose blend \
-define compose:args=50,50 -layers merge -alpha off tmp3.png
You are NOT understanding how image layer composition works!!!
Specifically it was really designed with image 'over' composition, and not blending.

NOTE: -composite is only one low level alpha composition of two images (and optional mask)

However ALL the layer commands involving N images actually use N+1 composite operations!!!!
For example....

Code: Select all

  convert A B -layers ????  result
where ??? is flatten, mosaic, or merge.

This will first look at the two images A, and B, and work out the size of the final image (as appropriate for the layering method which only vary in final image size).

It then creates an image of that size using -background color, and the meta-data of the first image (so as to preserve the first images meta-data).

Only then does it use -compose and -page to overlay A on that canvas, then perform a second compose with B. That is TWO compositions are performed.

So you are first doing a 50% blend of A with the background then making that a 50-50 blend with the second image. The result.. 25% background color 25% A and 50% B

If you want a true 50-50 blend then use -composite, or -average, but 'extend' all the images so they are the same size and positions first!

THE BETTER WAY
As I mentioned previously, the better way is to assigned the appropriate mask/alpha handling of the areas of overlap first, THEN just simply 'over' the images.

A overlay of an image with a 50% alpha transparency with a fully-opaque image will produce the equivalent of a 50-50 blend.

Another Alternative...
make all your images 50% transparent (-channel A evaluate multiply 0.5 +channel). Then 'merge' them with a background of 'none' using a -compose of 'plus'. After the merge turn off alpha. That will also generate a proper blended overlapped set of images, as long as only two images (not three or more) ever overlap.

Code: Select all

convert A B C  \
      -channel A evaluate multiply 0.5 +channel \
      -background none -compose Plus -layers merge \
      -alpha off   result
Using HDRI you don't even need to do that initial 50%

If images and the result could contain transparency, OR have more than two image overlaps, then you have to actually handle alpha division! to do a proper image mathematics to get this right. --- Better to then use -average (above).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compsite: using full canvas

Post by fmw42 »

Anthony, I cannot get a reasonable result with your -compose Plus approach, though I think it does make sense. It gives too bright an overlap area. Test these two and look at the results:


convert -background none \
-page -64-64 zelda3.png -page +0+0 lena2.png \
-compose blend -define compose:args=50,50 \
-layers merge -alpha off zelda_lena_blend50a.jpg

The above seems to give the correct 50/50 blend


convert -background none \
-page -64-64 zelda3.png -page +0+0 lena2.png \
-channel A -evaluate multiply 0.5 +channel \
-compose Plus -layers merge \
-alpha off zelda_lena_blend50b.jpg

The above seems to add the two images together in the overlap rather than blending.

Am I missing something or doing something wrong with your -compose Plus method?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: compsite: using full canvas

Post by anthony »

Code: Select all

convert granite: -page +100+20 rose: \
            -channel A -evaluate multiply 0.5 +channel \
           -background none  -compose Plus -layers merge \
            -alpha off show:
Hmmm strange. Okay yes I see, the alpha is being handled but the colors do not get the division they need in the overlay area.

Okay the above image is 170x128 pixels. (using a trick with -coalease, I fill out the canvas aof all images to that size. See http://www.imagemagick.org/Usage/layers/#coalesce

convert -page 170x128 granite: -page +100+20 rose: \
-background none -set dispose background -coalease \
-average -alpha off show:
Well that does not appear to be working. It looks as if average does not handle images with transparency right as it assumes the images are all fully opaque and does a global divide.

To do it properly we will need to do this ourselves. Add the colors (preventing overflow unless HDRI version is used) and a cound of the overlays (transparency add), Then divide the transparency.

I still thnk perhaps presetting the overlap transparency will work better.

What exactly do you want again. Overlapping photos, Or a blending with correct transparency handling (for panoramas)?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply