### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to generate multiple images/save to files .. with random noise ?  (Read 1127 times)

#### yahoo000

• New Member
• Posts: 13
##### How to generate multiple images/save to files .. with random noise ?
« on: March 05, 2021, 01:00:44 am »
How to generate multiple images/save to files .. with random noise ?

Is there any free component with some examples how to do it ?

so I got one original image "image1.jpg" and I would like to generate 300 images with random noise "image1_random1.jpg" "image1_random2.jpg"

thanks for help.

#### lainz

• Hero Member
• Posts: 4044
• Leandro Diaz
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #1 on: March 05, 2021, 02:04:40 am »
Yes, using BGRABitmap is easy

Color noise:

Code: Pascal  [Select][+][-]
1. procedure Noise(Bitmap: TBGRABitmap);
2. var
3.   i: integer;
4.   p: PBGRAPixel;
5. begin
6.   p := Bitmap.Data;
7.
8.   for i := Bitmap.NBPixels - 1 downto 0 do
9.   begin
10.     p^.red := Random(256);
11.     p^.green := Random(256);
12.     p^.blue := Random(256);
13.     //p^.alpha := Random(256);
14.     Inc(p);
15.   end;
16. end;

Black and white noise:

Code: Pascal  [Select][+][-]
1. procedure NoiseBW(Bitmap: TBGRABitmap);
2. var
3.   i: integer;
4.   p: PBGRAPixel;
5.   c: byte;
6. begin
7.   p := Bitmap.Data;
8.
9.   for i := Bitmap.NBPixels - 1 downto 0 do
10.   begin
11.     c := Random(2);
12.     p^.red := c + 255;
13.     p^.green := c + 255;
14.     p^.blue := c + 255;
15.     //p^.alpha := Random(256);
16.     Inc(p);
17.   end;
18. end;

Just do something like this:

Code: Pascal  [Select][+][-]
1. var
2.   i: integer;
3.   bmp: TBGRABitmap;
4. begin
5.   bmp := TBGRABitmap.Create(640,480);
6.   for i:=1 to 300 do
7.   begin
8.     NoiseBW(bmp);
9.     bmp.SaveToFile('image1_random' + i.ToString() + '.jpg');
10.   end;
11.   bmp.Free;
12. end;

Edit: add to uses clause BGRABitmap, BGRABitmapTypes.
« Last Edit: March 05, 2021, 02:07:03 am by lainz »

#### lainz

• Hero Member
• Posts: 4044
• Leandro Diaz
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #2 on: March 05, 2021, 02:08:19 am »
But I don't get if you want to generate random noise over the image with alpha perhaps?
- If this is the case, you can use alpha channel, load the image, and put noise over it.

Or generate noise with the pixels of the bitmap itelf?
- Maybe you can shuffle the array pixels somehow?

#### yahoo000

• New Member
• Posts: 13
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #3 on: March 05, 2021, 02:31:04 am »
But I don't get if you want to generate random noise over the image with alpha perhaps?
- If this is the case, you can use alpha channel, load the image, and put noise over it.

Or generate noise with the pixels of the bitmap itelf?
- Maybe you can shuffle the array pixels somehow?

But I don't get if you want to generate random noise over the image with alpha perhaps?
- If this is the case, you can use alpha channel, load the image, and put noise over it.

"yes exactly , over  the image" like in the below example

https://i.imgur.com/qgkUHtl.png

on the right you got noised original image but noised "strength=95"

I would like to generate let's say 300 images ... but with random noise strength "0.1 - 100" ..

#### lainz

• Hero Member
• Posts: 4044
• Leandro Diaz
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #4 on: March 05, 2021, 02:45:08 am »
That's easy too.

With the same noise function just provide the alpha channel in a parameter. Then blend your image with the noise and save it.

I'm not at the pc right now to write it. But you can do looking at the wiki examples maybe?

#### yahoo000

• New Member
• Posts: 13
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #5 on: March 05, 2021, 03:01:54 am »
That's easy too.

With the same noise function just provide the alpha channel in a parameter. Then blend your image with the noise and save it.

I'm not at the pc right now to write it. But you can do looking at the wiki examples maybe?

I cannot find it in wiki
can you show some example ?

thank you
« Last Edit: March 05, 2021, 01:44:42 pm by yahoo000 »

#### lainz

• Hero Member
• Posts: 4044
• Leandro Diaz
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #6 on: March 06, 2021, 05:08:01 pm »
Code: Pascal  [Select][+][-]
1. procedure NoiseBW(Bitmap: TBGRABitmap; Alpha: byte);
2. var
3.   i: integer;
4.   p: PBGRAPixel;
5.   c: byte;
6. begin
7.   p := Bitmap.Data;
8.
9.   for i := Bitmap.NBPixels - 1 downto 0 do
10.   begin
11.     c := Random(2);
12.     p^.red := c + 255;
13.     p^.green := c + 255;
14.     p^.blue := c + 255;
15.     p^.alpha := Alpha;
16.     Inc(p);
17.   end;
18. end;
19.
20. {\$R *.lfm}
21.
22. { TForm1 }
23.
24. procedure TForm1.Button1Click(Sender: TObject);
25. var
26.   i: integer;
27.   bmp, bmpNoise, bmpComposite: TBGRABitmap;
28. begin
29.   bmp := TBGRABitmap.Create('image1.jpg');
30.   bmpNoise := TBGRABitmap.Create(bmp.Width, bmp.Height);
31.   bmpComposite := TBGRABitmap.Create(bmp.Width, bmp.Height);
32.   for i:=1 to 10 do
33.   begin
34.     bmpNoise.FillTransparent();
35.     NoiseBW(bmpNoise, Random(256));
36.     bmpComposite.PutImage(0, 0, bmp, dmSet);
37.     bmpComposite.PutImage(0, 0, bmpNoise, dmDrawWithTransparency);
38.     bmpComposite.SaveToFile('image1_random' + i.ToString() + '.jpg');
39.   end;
40.   bmpComposite.Free;
41.   bmpNoise.Free;
42.   bmp.Free;
43. end;

#### yahoo000

• New Member
• Posts: 13
##### Re: How to generate multiple images/save to files .. with random noise ?
« Reply #7 on: March 06, 2021, 05:28:20 pm »
works great !!! thanks