Generate random noise over specific colors

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
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Generate random noise over specific colors

Post by BigNoub »

I'm using Imagemagick 7.0.8-14 on ubuntu 16.04

I would like to generate random noise:

Code: Select all

magick -size 100x100 xc: +noise Random random.png
but above specific colors, for instance noise over pastel colors, or reddish colors, instead of the usual RGB we get from the above command. Is there a way to do that? Apparently Random creates noise instead of adding it, so I tried with Uniform

Code: Select all

magick -size 100x100 xc:red +noise Uniform random.png 
but I got a fully red picture with no noise
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Generate random noise over specific colors

Post by snibgo »

I often ask two questions:

1. What effect do I want?

2. Where do I want it? Over all pixels, or just pixels of a certain colour, or what?

From your post, I'm unsure what your answers would be.

I don't know if you are trying to add noise to an image, or composite noise over an image. I show examples of each at Noise.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generate random noise over specific colors

Post by fmw42 »

I am not sure what you want to do. Do you want the random noise over certain regions of your image (by color) or do you want to add only noise of a certain color.

If the former, then create a mask from your input image where you want the noise to appear. Then create a same sized noise image. Then use the mask to compose plus (or screen or overlay) the noise image over your original image.


Input:
Image

Mask (created by +write in the code below)
Image


convert barn.jpg \
\( -clone 0 +noise random \) \
\( -clone 0 -fuzz 7% -fill black +opaque "rgb(131,168,238)" -fill white +opaque black +write barn_mask.png \) \
-compose plus -composite barn_result.jpg
Image


convert barn.jpg \
\( -clone 0 +noise random \) \
\( -clone 0 -fuzz 7% -fill black +opaque "rgb(131,168,238)" -fill white +opaque black +write barn_mask.png \) \
-compose screen -composite barn_result2.jpg
Image


convert barn.jpg \
\( -clone 0 +noise random \) \
\( -clone 0 -fuzz 7% -fill black +opaque "rgb(131,168,238)" -fill white +opaque black +write barn_mask.png \) \
-compose overlay -composite barn_result3.jpg
Image
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: Generate random noise over specific colors

Post by BigNoub »

Sorry for not being specific enough.

I'm just trying to generate backgrounds to be used later in other images, just like here: http://www.imagemagick.org/Usage/backgrounds/

The basis for most of these backgrounds is a random noise image, but because I can't control the colors of this noise I always end up with very brightly colored bacgkrounds such as this:

Image

So what I would like to do is to generate the same kind of image as this last one, but with pastel colors only, or with gradients of blue only, green only, etc..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Generate random noise over specific colors

Post by snibgo »

Why didn't you show the command you used?

You can process the output to make different results, for example:

Code: Select all

magick noise_hues.png -fill White -colorize 50 bn_pastel.png
Image

Code: Select all

magick noise_hues.png -channel B -separate +channel +level-colors black,blue blues.png
Image
snibgo's IM pages: im.snibgo.com
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: Generate random noise over specific colors

Post by BigNoub »

I showed the command I used to generate noise in my first post. I thought that would be enough, especially if there was a way to modify the color of the noise.

So yes I could process the output, but it multiplies my run time by 2 or 3. If there was a way to modify the colors in a previous step I think I would prefer that.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generate random noise over specific colors

Post by fmw42 »

Your commands do not produce the image you show. A random image would just be colored noise. How did you get it smoothed out. Please show the full command to produce the image you provided.


You can add attenuation to your command

Code: Select all

magick -size 100x100 xc: -attenuate 0.5 +noise Random random2.png
or you can modulate your command to either reduce the saturation or shift the colors or both

Code: Select all

magick -size 100x100 xc: +noise Random -modulate 100,50,50 random3.png
but these commands still make only a random noise image and are not smoothed out as shown in your example.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Generate random noise over specific colors

Post by snibgo »

The command you showed ...
BigNoub wrote:magick -size 100x100 xc: +noise Random random.png
... didn't make that image.

Most time in that command will be for writing the png file. Adding processing such as "-fill White -colorize 50" will hardly add any time.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generate random noise over specific colors

Post by fmw42 »

You can try Anthony's code from https://imagemagick.org/Usage/backgrounds/

Code: Select all

magick -size 100x100 xc: \
+noise Random -blur 0x10 -auto-level \
-separate -background white \
-compose ModulusAdd -flatten \
-channel R -combine +channel \
-set colorspace HSB -colorspace sRGB \
result.png
Each time you run it you will get different results unless you specify a -seed value before +noise.

Or try some of his other methods. But none as written allow you to specify only certain colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generate random noise over specific colors

Post by fmw42 »

If you want to specify your own colors, then you need to draw lots of points at random locations with the colors you want to use. Then do your blurring and other processing.
Post Reply