44
55class Fake_Camera ():
66
7- def __init__ (self , image = 'lena_color.jpg' , black_white = 1 , size = (1000 , 1000 ), add_noise = 1 ):
7+ def __init__ (self , image = 'lena_color.jpg' , colour = "c" , background_colour = "b" ,
8+ size = (1000 , 1000 ), add_noise = 0 , pixel_move = 5 ):
9+ """
10+ Library for creating a moving image on the screen.
811
9- self .image = image
10- self .black_white = black_white
11- self .canvas_size = size
12- self .add_noise = add_noise
13- self .flip_sign = 0
14- self .pixel_move = 5
15- self .main_config ()
16-
17- def main_config (self ):
18-
19- self .canvas = np .random .randint (1 , 255 , size = self .canvas_size , dtype = 'uint8' )
20- try :
21- self .frame = np .asarray ( Image .open ("lena_color.jpg" ).convert ('L' ) )
22-
23- except Exception :
24- self .frame = np .random .randint (1 , 255 , size = (int (self .canvas_size [0 ]/ 2 ), int (self .canvas_size [1 ]/ 2 ) ), dtype = 'uint8' )
12+ args:
13+ image = ".jpg" Image to be displayed
14+ colour = "c" or "g" for colour or grayscale
15+ backgroung_colour = "w", "b", "r" for white, black, random
16+ size = (1000,1000) Size of the Canvas
17+ add_noise = "Yes", "No" Add noise to the images
18+ pixel_move = int() How much pixels the image moves
19+ """
2520
26- if self .black_white == 1 :
27- self .config_black_white ()
28-
21+ self .image = image
22+ self .colour = colour
23+ self .background_colour = background_colour
24+ self .canvas_size = size
25+ self .add_noise = add_noise
26+ self .pixel_move = pixel_move
27+ self .flip_sign = 0
28+ self .image_config ()
29+ self .background_config ()
30+ self .canvas_config ()
2931 if self .add_noise :
3032 self .config_noise ()
31-
32- def config_black_white (self ):
33+
34+ def image_config (self ):
35+ if self .colour == "g" :
36+ try :
37+ self .frame = np .asarray ( Image .open (self .image ).convert ('L' ) )
38+ except Exception :
39+ print ("Image does not exist in the current directory!" )
40+ return
41+
42+ elif self .colour == "c" :
43+ try :
44+ self .frame = np .asarray ( Image .open (self .image ) )
45+ self .frame = self .frame [...,[2 ,1 ,0 ]]
46+ except Exception :
47+ print ("Image does not exist in the current directory!" )
48+ return
49+
50+ def background_config (self ):
51+
52+ if self .colour == "g" :
53+ if self .background_colour == "r" :
54+ self .canvas = np .random .randint (1 , 255 , size = self .canvas_size , dtype = 'uint8' )
55+ if self .background_colour == "w" :
56+ self .canvas = np .ones (self .canvas_size , dtype = 'uint8' ) * 255
57+ if self .background_colour == "b" :
58+ self .canvas = np .zeros (self .canvas_size , dtype = 'uint8' )
59+
60+ if self .colour == "c" :
61+ if self .background_colour == "r" :
62+ self .canvas = np .random .randint (1 , 255 , size = self .canvas_size + (3 ,), dtype = 'uint8' )
63+ if self .background_colour == "w" :
64+ self .canvas = np .ones (self .canvas_size + (3 ,), dtype = 'uint8' ) * 255
65+ if self .background_colour == "b" :
66+ self .canvas = np .zeros (self .canvas_size + (3 ,), dtype = 'uint8' )
67+
68+ def canvas_config (self ):
3369
3470 self .upper_left_point_x = int ( self .canvas .shape [0 ] / 4 )
3571 self .upper_left_point_y = int ( self .canvas .shape [0 ] / 4 )
@@ -44,18 +80,25 @@ def config_black_white(self):
4480
4581 self .canvas_view = self .canvas [ self .limit_x_start : self .limit_x_end , self .limit_y_start : self .limit_y_end ]
4682 self .old_start_x = self .upper_left_point_x
47- self .old_start_y = self .upper_left_point_y
83+ self .old_start_y = self .upper_left_point_y
4884
4985 def config_noise (self ):
50- self .noise_start = np .zeros ( ( self .canvas_view .shape [0 ],self .canvas_view .shape [1 ] ), np .uint8 )
5186 self .mean = 0
5287 self .var = 0.1
5388 self .sigma = self .var ** 0.5
5489
55- def add_noise_to_image (self , image ):
56- row , col = image .shape
57- self .gauss = np .random .normal ( self .mean , self .sigma , (row , col ) ) * 50
58- self .gauss = self .gauss .reshape ( row , col )
90+ def add_noise_to_image (self , image ):
91+
92+ if len (image .shape ) == 2 :
93+ row , col = image .shape
94+ self .gauss = np .random .normal ( self .mean , self .sigma , (row , col ) ) * 50
95+ self .gauss = self .gauss .reshape ( row , col )
96+
97+ elif len (image .shape ) == 3 :
98+ row , col , ch = image .shape
99+ self .gauss = np .random .normal ( self .mean , self .sigma , (row , col , ch ) ) * 50
100+ self .gauss = self .gauss .reshape ( row , col , ch )
101+
59102 self .gauss = self .gauss .astype ("uint8" )
60103 noisy_image = image + self .gauss
61104 return noisy_image
@@ -94,4 +137,4 @@ def read_fake_image(self):
94137
95138 except Exception :
96139 print ("Error Somewhere" )
97- #---------------------------------------------------------------------------------------------------------------------------#
140+ #-----------------------------------------------------------------------------#
0 commit comments