-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeImage.py
54 lines (42 loc) · 1.48 KB
/
changeImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import os
import glob
from PIL import Image
PATH = "supplier-data/images/"
def change_image(source_dir: str, dest_dir: str) -> None:
"""
Changes TIFF images in source_dir: resizing to (600, 400) and changing format to JPEG.
Then saves modified images in dest_dir.
"""
error = False
try:
if not os.path.exists(source_dir):
raise FileExistsError
if not os.path.isdir(source_dir):
raise NotADirectoryError
for image in glob.glob(source_dir + '*.tiff'):
base = os.path.basename(image)
name = os.path.splitext(base)[0]
with Image.open(image) as img:
if img.mode != 'RGB':
img = img.convert('RGB')
new_img = img.resize((600, 400))
new_img.save(dest_dir + name + '.jpeg', 'JPEG')
except FileExistsError:
print(f"[Error] Not such directory: '{source_dir}'")
error = True
except NotADirectoryError:
print(f"[Error] '{source_dir}' is a file, not a directory")
error = True
except FileNotFoundError:
print(f"[Error] Not such directory: '{dest_dir}'")
error = True
except PermissionError:
print(f"[Error] Permission denied to write in directory: '{dest_dir}'")
error = True
finally:
pass
if not error:
print("Image transformation finished successfully")
# modify images in the same directory
change_image(PATH, PATH)