Skip to content

Commit 0920c29

Browse files
committed
continue script if cant identify image file
1 parent ab9f6b3 commit 0920c29

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

imgwebpconverter.py

+21-17
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,27 @@ def convert_to_webp(path, quality):
1414
# Full path of the image file
1515
image_path = os.path.join(root, file)
1616

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
3438

3539
if __name__ == "__main__":
3640
# Set up command-line argument parsing

0 commit comments

Comments
 (0)