how to create an empty layer with the same size?

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
pht

how to create an empty layer with the same size?

Post by pht »

Hello. I'm using this code to add some random black pixels to an image:

Code: Select all

convert png:input.png 
         \( -size 909x516 xc:black -matte 
            -channel Alpha +noise Random -threshold 2% \)
        -flatten  png:- > noised.png
The question is how do I eliminate the need for exact dimensions in the -size parameter? Ie. how to create a new layer that has exactly the same dimensions as another layer (input.png)? I've thought of -cloning and -drawing a black rectangle, but that would be IMHO too ineffective. Any ideas?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to create an empty layer with the same size?

Post by anthony »

Basically you clear the image (and optionally its meta-data)

For example

Code: Select all

convert png:input.png
         \( +clone -alpha opaque  +level-colors black
            -channel A +noise Random -threshold 2% \)
        -flatten  noised.png
this used the newer -alpha and +level-colors methods.
Older method for these two operators would be

Code: Select all

   +matte -matte -fill black -colorize 100%
The two matte commands ensure the image is made fully-opaque
thought considering you are going to fill the alpha with noise I don't think that matters.

also as you are overlaying striping the meta-data also does not matter, in the above.

See IM Examples, Canvas generation, Blanking Existing Images
http://www.imagemagick.org/Usage/canvas/#blank
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pht

Re: how to create an empty layer with the same size?

Post by pht »

Just as I suspected, -clone and -colorize add some time to the toll. But that being just 3% of the total time, I guess can be lived with. Thanks!

The test:

Code: Select all

$ time for i in `seq 1 100`; do convert png:motd.png \( -clone 0 -fill black -colorize 100% -matte -channel Alpha +noise Random -threshold 2% \)  -flatten  png:- > a.png; done

real    1m9.327s
user    1m3.476s
sys     0m4.050s

$ time for i in `seq 1 100`; do convert png:motd.png \( -size 909x516 -matte xc:black -channel Alpha +noise Random -threshold 2% \)  -flatten  png:- > a.png; done

real    1m6.360s
user    1m1.033s
sys     0m3.603s
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to create an empty layer with the same size?

Post by anthony »

-colorize is I believe not as fast as +level-colors however
-evaluate set 0 may be faster. You can also try -gamma 0 as a less obvious way of setting all colors to black!

The known methods are all listed in IM Examples, Canvas Generation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply