Page 1 of 1

how to create an empty layer with the same size?

Posted: 2008-11-18T00:59:56-07:00
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?

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

Posted: 2008-11-18T01:06:05-07:00
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

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

Posted: 2008-11-18T01:28:45-07:00
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

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

Posted: 2008-11-18T16:40:14-07:00
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.