1+ # HW9-method1 (Generation of zero mean Gaussian noise)
2+
3+ from PIL import Image
4+ import numpy as np
5+ import matplotlib .pyplot as plt
6+
7+ # 1. Create an image g and the same gray values of 100
8+ g = Image .open ("g.png" ).convert ("L" )
9+ for i in range (g .size [0 ]):
10+ for j in range (g .size [1 ]):
11+ g .putpixel ((i , j ), 100 )
12+
13+ g .save ("g.png" )
14+ g .show ()
15+
16+ # Save image g histogram
17+ a = np .array (g )
18+ plt .hist (a .ravel (), bins = 256 )
19+ plt .ylabel ('Number of pixels' )
20+ plt .xlabel ('Gray Scale' )
21+ plt .savefig ('g_histogram.png' )
22+ plt .show ()
23+
24+ # 2. Generate Gaussian noise n and Show noisy image f
25+ # Calculate Gaussian noise with menu=0 and variance=15
26+ menu = 0
27+ variance = 15
28+ noise = np .random .normal (menu , variance , g .size )
29+ # Add the noise to the image g
30+ f_array_noise = np .add (np .array (g ), noise ) # f = g + noise
31+ f = Image .fromarray (f_array_noise )
32+ # Show the noisy image f
33+ f .show ()
34+
35+ # 3. Save and display noisy image f histogram
36+ a = np .array (f )
37+ plt .hist (a .ravel (), bins = 256 )
38+ plt .ylabel ('Number of pixels' )
39+ plt .xlabel ('Gray Scale' )
40+ plt .savefig ('f_histogram.png' )
41+ plt .show ()
0 commit comments