1- import os
2- import json
1+ """
2+ TD
3+ """
4+
5+ # Python Standard Libraries
36import base64
7+ import json
8+ import os
49from time import sleep
10+
11+ # Third-Party Libraries
12+ from cairosvg import svg2png
13+ from PIL import Image
514from selenium import webdriver
615from selenium .webdriver .chrome .service import Service
716from selenium .webdriver .chrome .options import Options
8- from PIL import Image
9- from cairosvg import svg2png
1017
1118
12- def get_screenshot (url , wait , directory , settings_devices ):
1319
20+
21+
22+
23+
24+
25+
26+
27+ def get_screenshot (url , wait , directory , settings_devices ):
28+ """
29+ TD
30+ """
1431 # Set options
1532 options = Options ()
1633 options .add_argument (f"--window-size={ settings_devices ['width_large' ]} ,{ settings_devices ['height_large' ]} " )
17- options .add_argument (f "--no-sandbox" )
18- options .add_argument (f "--headless" )
19- options .add_argument (f "--hide-scrollbars" )
34+ options .add_argument ("--no-sandbox" )
35+ options .add_argument ("--headless" )
36+ options .add_argument ("--hide-scrollbars" )
2037
2138 try :
2239 # Set Chromedriver path
@@ -30,7 +47,7 @@ def get_screenshot(url, wait, directory, settings_devices):
3047
3148 # Open Chrome webdriver
3249 driver = webdriver .Chrome (options = options )
33-
50+
3451 # Take screenshot
3552 driver .get (url )
3653 sleep (wait )
@@ -46,11 +63,13 @@ def get_screenshot(url, wait, directory, settings_devices):
4663
4764
4865def get_screenshot_full (url , wait , directory , settings_devices ):
49-
66+ """
67+ TD
68+ """
5069 options = Options ()
51- options .add_argument (f "--no-sandbox" )
52- options .add_argument (f "--headless=new" )
53- options .add_argument (f "--hide-scrollbars" )
70+ options .add_argument ("--no-sandbox" )
71+ options .add_argument ("--headless=new" )
72+ options .add_argument ("--hide-scrollbars" )
5473
5574 try :
5675 # Set Chromedriver path
@@ -60,7 +79,7 @@ def get_screenshot_full(url, wait, directory, settings_devices):
6079 driver = webdriver .Chrome (service = service , options = options )
6180 except :
6281 # Open Chrome webdriver
63- driver = webdriver .Chrome (options = options )
82+ driver = webdriver .Chrome (options = options )
6483
6584 # Open Chrome webdriver
6685 driver = webdriver .Chrome (options = options )
@@ -70,7 +89,7 @@ def get_screenshot_full(url, wait, directory, settings_devices):
7089 sleep (wait )
7190
7291 fname_out_full_screenshot_png = settings_devices ["filename_large" ]
73-
92+
7493 png = get_screenshot_full_chrome (driver )
7594 with open (f"{ directory } /{ fname_out_full_screenshot_png } " , 'wb' ) as f :
7695 f .write (png )
@@ -81,8 +100,10 @@ def get_screenshot_full(url, wait, directory, settings_devices):
81100
82101
83102def get_screenshot_full_chrome (driver ) :
84-
85- # Function adapted from StackOverflow answer
103+ """
104+ TD
105+ """
106+ # Function adapted from StackOverflow answer
86107 # https://stackoverflow.com/questions/45199076/take-full-page-screenshot-in-chrome-with-selenium/45201692#45201692
87108
88109 def send (cmd , params ):
@@ -120,14 +141,16 @@ def evaluate(script):
120141
121142
122143def get_base (post , directory , svg , svg_fname , png_fname ):
123-
144+ """
145+ TD
146+ """
124147 # Create SVG file
125- with open (f"{ directory } /{ svg_fname } " , "w" ) as file :
148+ with open (f"{ directory } /{ svg_fname } " , "w" , encoding = "utf-8" ) as file :
126149 file .write (svg )
127-
150+
128151 # Convert to SVG to PNG
129- svg2png (url = f"{ directory } /{ svg_fname } " ,
130- write_to = f"{ directory } /{ png_fname } " ,
152+ svg2png (url = f"{ directory } /{ svg_fname } " ,
153+ write_to = f"{ directory } /{ png_fname } " ,
131154 background_color = post ["doc_fill_color" ])
132155
133156 # Delete SVG file
@@ -137,12 +160,14 @@ def get_base(post, directory, svg, svg_fname, png_fname):
137160
138161
139162def get_overlay (directory , fname_input , fname_output , new_width , height_crop ):
140-
163+ """
164+ TD
165+ """
141166 # Open the PNG image
142167 image = Image .open (f"{ directory } /{ fname_input } " )
143168
144169 # Determine aspect ratio
145- aspect_ratio = image .height / image .width
170+ aspect_ratio = image .height / image .width
146171
147172 # Set new height
148173 new_height = int (new_width * aspect_ratio )
@@ -161,7 +186,9 @@ def get_overlay(directory, fname_input, fname_output, new_width, height_crop):
161186
162187
163188def get_final_temp (base , overlay , lat , lng , directory_path , new_file_name ):
164-
189+ """
190+ TD
191+ """
165192 # Open the base image
166193 base_image = Image .open (base )
167194
@@ -181,34 +208,37 @@ def get_final_temp(base, overlay, lat, lng, directory_path, new_file_name):
181208
182209
183210def get_final (directory , filename_input , filename_output , post ):
184-
211+ """
212+ TD
213+ """
185214 right = post ["doc_pad_h" ]
186215 left = post ["doc_pad_h" ]
187216 top = post ["doc_pad_v" ]
188217 bottom = post ["doc_pad_v" ]
189218 color = post ["doc_fill_color" ]
190219
191- image = Image .open (f"{ directory } /{ filename_input } " )
192-
193- width , height = image .size
194-
195- new_width = width + right + left
196- new_height = height + top + bottom
197-
198- result = Image .new (image .mode , (new_width , new_height ), color )
199-
200- result .paste (image , (left , top ))
201-
202- result .save (f"{ directory } /{ filename_output } " )
220+ image = Image .open (f"{ directory } /{ filename_input } " )
221+
222+ width , height = image .size
223+
224+ new_width = width + right + left
225+ new_height = height + top + bottom
226+
227+ result = Image .new (image .mode , (new_width , new_height ), color )
228+
229+ result .paste (image , (left , top ))
230+
231+ result .save (f"{ directory } /{ filename_output } " )
203232
204233 return 1
205234
206235
207236def cleanup (directory_path , file ):
237+ """
238+ TD
239+ """
208240 try :
209241 os .remove (f"{ directory_path } /{ file } " )
210242 return f"{ file } has been deleted"
211243 except FileNotFoundError :
212244 return f"{ file } not in directory"
213-
214-
0 commit comments