Skip to content

Commit 79eafda

Browse files
committed
Handled unsupported file types, such as .pdf
1 parent f709c7d commit 79eafda

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

IMAGES & PHOTO SCRIPTS/Image-Inverter/inverter.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22

3-
from PIL import Image
4-
from PIL import ImageOps
3+
from PIL import Image, ImageOps, UnidentifiedImageError
54
import sys, os
65

76
def check_input():
@@ -27,12 +26,14 @@ def main():
2726

2827
image_path_no_ext, extension = os.path.splitext(file)
2928

30-
with Image.open(file) as image:
31-
new_path_with_ext = image_path_no_ext + "_inverted" + extension
32-
ImageOps.invert(image).save(new_path_with_ext)
33-
if verbose_enabled:
34-
print("Successfully inverted " + file + "\n" + new_path_with_ext + " is generated.\n")
35-
29+
try:
30+
with Image.open(file) as image:
31+
new_path_with_ext = image_path_no_ext + "_inverted" + extension
32+
ImageOps.invert(image).save(new_path_with_ext)
33+
if verbose_enabled:
34+
print("Successfully inverted " + file + "\n" + new_path_with_ext + " is generated.\n")
35+
except UnidentifiedImageError:
36+
print(file + " is not suppotred, please provide a supported file type.")
3637
i = i + 1
3738

3839
if __name__ == '__main__':

0 commit comments

Comments
 (0)