-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpng_To_jpeg_converter.py
More file actions
74 lines (52 loc) · 2.28 KB
/
png_To_jpeg_converter.py
File metadata and controls
74 lines (52 loc) · 2.28 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# png_To_jpeg_converter.py
# This script converts a png image file to a jpeg image file and
# saves the converted image to a new file.
# Written by: JakeTheSnake(JMG3000)
from PIL import Image
import os
def converter():
stop = True
while stop:
# define the path to the desired folder
pic_path = os.path.expanduser('~/Pictures/headshots')
user_png_path = input("Please enter the path and name of the png you wish to convert: ")
if user_png_path == "":
input_path = pic_path + "/closer_headshot_blackSuitAndRedTie.png"
else:
input_path = user_png_path
user_jpeg_path = input("Please enter the path and name of the converted jpeg you wish to save: ")
if user_jpeg_path == "":
output_path = pic_path + "/closer_pro_headshot_high_quality.jpg"
else:
output_path = user_jpeg_path
# attempt to open the file
try:
img_data = Image.open(input_path)
print("Opening {}".format(input_path))
# catch an exception when the original file is not opened
except Exception as e:
print(f"Failed to open {input_path}. Error Code{e}")
break
# attempt to convert the file after opening is successful
try:
img_data = img_data.convert("RGB")
print("Converting {}".format(input_path))
# catch an exception when the original file is not converted
except Exception as e:
print(f"Failed to convert {input_path}. Error Code{e}")
# attempt to save the file after convertion is successful
try:
img_data.save(output_path, "JPEG", quality=95)
print("Saved {}".format(output_path))
return 1
# catch an exception when the converted file is not saved
except Exception as e:
print(f"Failed to save {output_path}. Error Code{e}")
return 0
if __name__ == "__main__":
print("...Done")
if converter() == 1:
print("The png image has been converted to a jpeg!")
else:
print("An Error has occurred in the application.\n The png image has not been converted to a jpeg!\n")