convert command with noise overlay

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
sirchuck
Posts: 6
Joined: 2019-05-21T19:26:44-07:00
Authentication code: 1152

convert command with noise overlay

Post by sirchuck »

Ubuntu 18.04
ImageMagick 6.9.7-4 x86_64
PHP

I have some code to develop some colored blocks, but I'd like to add a bit of texture. I don't want to use a script, I just want to do it in one line. So I'm thinking just add a little noise with lowered opacity as an overlay.

The code looks like this now.
exec('convert -font Times-Bold -pointsize 24 -gravity center -fill "rgba(0,0,0,0.40)" \
\( -size 1x25 xc:#000000 \) \
\( -size 1x25 xc:#' . $this->_m->random_color() . ' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 25x25 xc:#'. $dot_colors[$r1] .' -annotate +0+2 \''.$dot_txt[$r1].'\' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 1x25 xc:#' . $this->_m->random_color() . ' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 25x25 xc:#'. $dot_colors[$r2] .' -annotate +0+2 \''.$dot_txt[$r2].'\' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 1x25 xc:#' . $this->_m->random_color() . ' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 25x25 xc:#'. $dot_colors[$r3] .' -annotate +0+2 \''.$dot_txt[$r3].'\' \) \
\( -size 1x25 xc:#000000 \) \
\( -size 1x25 xc:#' . $this->_m->random_color() . ' \) \
\( -size 1x25 xc:#000000 \) \
+append /tmp/capimg.png');

How could I add a noise layer to it with say 20% opacity? Ignore the php variables, they are just arrays for color and text.

The noise or maybe plasma:fractal overlay would have to start at the top left of the created image. I assume the code would come after the append?

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

Re: convert command with noise overlay

Post by fmw42 »

Assume you know the size of your output. Here is an example adding plasma fractal noise to the lena image.

Image

Code: Select all

convert lena.png \
\( -size 256x256 plasma: -colorspace gray -alpha set -channel a -evaluate multiply 0.3 +channel \) \
-compose over -composite \
lena_result.png
Image

You simply create a plasma image the size of your image, convert to grayscale if you want, then enable the alpha channel, then select the alpha channel, multiply by 0.2, then enable all channels. Then you composite that over your image.

If you do not know your output size, then you can just use gaussian noise

Code: Select all

convert lena.png \
\( +clone -fill gray -colorize 100 +noise gaussian -colorspace gray -alpha on -channel a -evaluate multiply 0.3 +channel \) \
-compose over -composite \
lena_result2.png
Image

You can emulate the plasma type result if you do not know the output size, by blurring the gaussian noise example:

Code: Select all

convert lena.png \
\( +clone -fill gray -colorize 100 +noise gaussian -colorspace gray -blur 0x2 -level 20x80%  -alpha on -channel a -evaluate multiply 0.3 +channel \) \
-compose over -composite \
lena_result3.png
Image

Note that in IM 7, you can get the size automatically, so that my first method becomes:

Code: Select all

magick lena.png \
\( +clone -size "%wx%h" plasma: -colorspace gray -alpha set -channel a -evaluate multiply 0.5 +channel -delete 0 \) \
-compose over -composite \
lena_result7.png
The +clone is needed just to get the dimensions, since the lena image was outside the parentheses. But I do -delete 0 to remove it once its dimensions have been extracted.
sirchuck
Posts: 6
Joined: 2019-05-21T19:26:44-07:00
Authentication code: 1152

Re: convert command with noise overlay

Post by sirchuck »

How do I attach that to the single command above? Is lena.png a sort of temporary file convert uses, or is that supposed to be a real image I have already? Basically, I just want to add some texture to the image I am creating with the converted code I created. I figured adding noise would be the easiest way.
sirchuck
Posts: 6
Joined: 2019-05-21T19:26:44-07:00
Authentication code: 1152

Re: convert command with noise overlay

Post by sirchuck »

Oh, ok so lena was the image you used. I guess I can add your code after my +appned maybe? I'll give that a try.
sirchuck
Posts: 6
Joined: 2019-05-21T19:26:44-07:00
Authentication code: 1152

Re: convert command with noise overlay

Post by sirchuck »

Got it, thanks :D
Post Reply