@@ -14,23 +14,27 @@ def convert_to_webp(path, quality):
14
14
# Full path of the image file
15
15
image_path = os .path .join (root , file )
16
16
17
- # Open the image and convert it to WebP
18
- with Image .open (image_path ) as img :
19
- # Define the destination path with .webp extension
20
- webp_path = os .path .splitext (image_path )[0 ] + '.webp'
21
- if os .path .exists (webp_path ):
22
- print (f'Skipping { image_path } , WebP version already exists.' )
23
- continue # Skip to the next file
24
-
25
- try :
26
- # Save the image in WebP format with the specified quality
27
- img .save (webp_path , 'webp' , quality = quality )
28
- os .remove (image_path )
29
- except Exception as e :
30
- print (f'Error converting { image_path } to WebP: { e } ' )
31
- continue
32
-
33
- print (f'Converted: { image_path } -> { webp_path } ' )
17
+ try :
18
+ # Open the image and convert it to WebP
19
+ with Image .open (image_path ) as img :
20
+ # Define the destination path with .webp extension
21
+ webp_path = os .path .splitext (image_path )[0 ] + '.webp'
22
+ if os .path .exists (webp_path ):
23
+ print (f'Skipping { image_path } , WebP version already exists.' )
24
+ continue # Skip to the next file
25
+
26
+ try :
27
+ # Save the image in WebP format with the specified quality
28
+ img .save (webp_path , 'webp' , quality = quality )
29
+ os .remove (image_path )
30
+ except Exception as e :
31
+ print (f'Error converting { image_path } to WebP: { e } ' )
32
+ continue
33
+
34
+ print (f'Converted: { image_path } -> { webp_path } ' )
35
+ except Exception as e :
36
+ print (f'Error opening { image_path } : { e } ' )
37
+ continue
34
38
35
39
if __name__ == "__main__" :
36
40
# Set up command-line argument parsing
0 commit comments