[SOLVED] composite -blend 1% [and how to add "darken only"]?

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?".
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

[SOLVED] composite -blend 1% [and how to add "darken only"]?

Post by jkop »

Hello :)

I'm trying to superimpose a batch of hundred black and white images so that they blend with 1% opacity and "darken only". The multiplicity of the black parts should become visible without being covered by the white parts. Previously I have opened each and every image in Gimp, pasted them into separate layers, and set the layers to 1% opacity and "darken only". However, you can imagine this is quite boring work. With Imagemagick I have tried "composite -blend 1%", but I can't seem to figure out how to get "darken only". Please help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

darken only is

convert image1 image2 -compose darken -composite resultimage

I believe you can have more that two images.

Also this is better,

convert image1 image2 ... imageN -evaluate-sequence min resultimage

see

http://www.imagemagick.org/Usage/compose/#darken
http://www.imagemagick.org/script/comma ... e-sequence
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

Re: composite -blend 1% [and how to add "darken only"]?

Post by jkop »

Thanks :) now I can also darken images to resultimage. However, I need to darken and blend to resultimage. Is it possible?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

jkop wrote:Thanks :) now I can also darken images to resultimage. However, I need to darken and blend to resultimage. Is it possible?

The darken process for each pixel picks the darkest value from all the images. Thus the resulting image is coming from a mix of every image on a pixel by pixel basis?

Please clarify how you need to blend as well as darken. Is this kind of darkening, which mixes data from many images not the kind of blending or darkening that you need?

If you want to darken each image on the basis of the local area, ie. take the darkest pixel in the neighborhood, then see

convert image -statistic minima MxN resultimage

http://www.imagemagick.org/script/comma ... #statistic
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

Re: composite -blend 1% [and how to add "darken only"]?

Post by jkop »

The batch of images are black and white and depict the shadow-shapes cast by a group of buildings: one shadow every 6th minute, from 8 am to 6 pm, which means 100 images of shadows, each slightly different from the other because of the rotation of the earth. Now if I superimpose them in such a way that each image has only 1% opacity (i.e. almost transparent), plus disregard the white in each image, then the areas which are overlapped by all 100 shadows will be 100% dark, whereas other areas will be grey in various degrees depending on the amount of overlaps. The resulting superimposition can give me a hint on where to put plants, or trees, and where I can minimize areas which would recieve no sun light.

For example, would there be another way to disregard the white in each image before or together with the -blend command? Perhaps make all white transparent and then blend all 100 with 1%?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

Are your images actually binary b/w or grayscale 8-bit? This has to do with your concept and also what you consider as white?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: composite -blend 1% [and how to add "darken only"]?

Post by anthony »

jkop wrote:Thanks :) now I can also darken images to resultimage. However, I need to darken and blend to resultimage. Is it possible?
To darken and blend, you will want to 'burn' images together.
See IM Examples, Composion, Lifghting methods
http://www.imagemagick.org/Usage/compose/#light

The most common 'burn' compose method is 'mutliply' (which is also a mathematical method)
http://www.imagemagick.org/Usage/compose/#multiply
but there are many other 'lighting' methods that can be used, such as Linear_Burn
http://www.imagemagick.org/Usage/compose/#linearburn

However as you specify you are dealing with black and white images only (the circles below are NOT B&W but greyscale), all you really need is to use +level to specify the grayscale for the black areas, and the compose them together. As multiple images are being is -flatten can be used.

Example... two circles (black on white)

Code: Select all

convert -size 64x64 xc: -draw 'circle 22,32 5,32' circle_1.png
convert -size 64x64 xc: -draw 'circle 41,32 58,32' circle_2.png
Image Image

Now set the gray level (blend amount) and merge the images using multiply. Flatten used for composition so more than two images can be handled. (Note a white starting canvas background is needed for multiply)

Code: Select all

convert circle_*.png  +level 70%,100% \
            -background white -compose multiply -flatten \
            circles_blended.png
Image

Adjust your blend levels as you like!
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: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

Is one option to -evaluate-sequence <multiply>? That would allow multiple images to be accumulated without having to do them pairwise with -compose multiply -composite.


convert -size 100x100 xc:gray gray.png

convert gray.png gray.png -evaluate-sequence multiply gray_mult2.png


The result I would expect would be 25% (=50%*50%), but the result is pure white and no error message. So is this working correctly?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: composite -blend 1% [and how to add "darken only"]?

Post by anthony »

ASIDE, and probably a little off topic...

An -evaluate-sequence multiply (if implemented) would be equivalent to a -flatten but without a starting canvas.
The same goes for 'Add', 'Min' and 'Max', Actually I would not be surprised if they are implemented as compositions.

The only reason the original 'average' and 'median' could not be made a compose methods was that they can not be implemented as a sequence of operations, but needs extra handling.

Actually I would like to see it expanded to do exactly that with any compose method. That is flatten without a starting canvas.
A 'minus' type flatten (subtract later images from first image) could then be properly implemented, something that is not currently possible

Actually even that is possible with -flatten by the trickly use of "linear_burn" and the negation of all but the first image with a white background). I did say it was tricky!
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: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

An -evaluate-sequence multiply (if implemented) would be equivalent to a -flatten but without a starting canvas.
Thanks for the explanation.

Yes, that does sound about right ===> ... -compose multiply -flatten ... .
I had not thought of it that way.

But I guess I will report a potential bug for -evaluate-sequence multiply, anyway.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: composite -blend 1% [and how to add "darken only"]?

Post by anthony »

Does -evaluate-sequence actually have multiply as a option?

What is definitely a bug is that -list currently does not list the options for evaluate-sequence That is -list list does not list EvaluateSequence as a possibile option to -list.

But enough of this side topic... Is jkop happy with the solution to use -flatten with a -compose multiply as the solution to his blending problem.
jkop wrote:The batch of images are black and white and depict the shadow-shapes cast by a group of buildings: one shadow every 6th minute, from 8 am to 6 pm, which means 100 images of shadows, each slightly different from the other because of the rotation of the earth. Now if I superimpose them in such a way that each image has only 1% opacity (i.e. almost transparent), plus disregard the white in each image, then the areas which are overlapped by all 100 shadows will be 100% dark, whereas other areas will be grey in various degrees depending on the amount of overlaps. The resulting superimposition can give me a hint on where to put plants, or trees, and where I can minimize areas which would recieve no sun light.
Actually looking at his later description. What is really asking for is a 'subtract 1%' for every black areas in each image, which is exactly a linear_burn using negated images does! Note that the shadow images, as he described them, are already negated in that we are subtracting the black component from the initial white canvas, so it is a straight forward solution.

Code: Select all

convert  shadow_*.png  +level 99%,100% \
            -compose linear_burn -background white -flatten \
            shadow_merge_result.png
Of course the only real test is with the actual images involved.

The other way to look at this, would be to negate and scale all the shadow images (white on black), add them together, and negate the result.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

Re: composite -blend 1% [and how to add "darken only"]?

Post by jkop »

Thanks very much for your comments :)

I tried

convert *.png +level 99%,100% \
-compose linear_burn -background white -flatten \
shadow_merge_result.png

on a test batch of 10 images, and it seems it produced the desired result, and quite fast as well, which made me happy. However, when I tried it on the batch of 100 images my computer's memory usage hit the sky and beyond :/ ..so I had to abort it.

I recall a similar memory problem when using convert -threshold on my work computer, and in spite of its 16 gb of memory I had to abort it. I resolved to use mogrify -threshold instead. Would linear_burn work with mogrify? (I can't test it at the moment).

Thanks!
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

Re: composite -blend 1% [and how to add "darken only"]?

Post by jkop »

P.S.
The images are 8 bit grayscale, but each image is "thresholded" to black and white. Their superimposition, however, will create areas of gray depending on amount of overlaps in those areas (if each image is set to 1% opacity).

Later I should also try

convert *.png +level 70%,100% \
-background white -compose multiply -flatten \
images_blended.png

..unless the convert command hogs my computer's memory.
jkop
Posts: 10
Joined: 2011-09-27T07:27:05-07:00
Authentication code: 8675308

Re: composite -blend 1% [and how to add "darken only"]?

Post by jkop »

Ok now I have made tests on my work computer on all 100 images with

convert *.png +level 99%,100% \
-compose linear_burn -background white -flatten \
shadow_merge_result.png

but the images might be too large, 3000x5000 pixels each, for the computer runs out of memory. If 16 GB RAM + 5 GB swap is not sufficient, then it seems I should avoid the convert command and look for another.

I replaced convert with mogrify in the above command, and although it made IM process each image (without the immense usage of memory) the result is not the overlapping of shadows as the convert command produced on the smaller batch earlier.

Hmm.. isn't there some way to just get rid of all white (perhaps convert all to gif with white as transparent or the like) and then I could perhaps use the composite -blend 1%... ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite -blend 1% [and how to add "darken only"]?

Post by fmw42 »

run script loop and do the -compose multiply (or whatever) pair-wise using the previous result as one of the pair and then next image in your sequence as the second of the pair. That way, you do not have to hold all the images in memory at the same time.

Making images transparent is not going to make it faster or take up less memory.
Post Reply