-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoPNG.py
58 lines (36 loc) · 1.45 KB
/
toPNG.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
from PIL import Image
import os
import getopt, sys
def convertToPNG(folderPath):
"""A function that replaces every image in a folder with a png conversion"""
fileNames = os.listdir(folderPath)
count = 0
for filename in fileNames:
if filename.split('.')[1] not in {'jpg', 'jpeg'}: continue
count += 1
# load the original image
non_png_filepath = os.path.join(folderPath, filename)
with Image.open(non_png_filepath) as non_png_img:
# convert the original image
png_img = non_png_img.convert("RGBA")
new_img_name = filename.split('.')[0] + '.png'
# save the new image in the same directory
png_img.save(os.path.join(folderPath, new_img_name))
# delete the old image
os.remove(non_png_filepath)
return count
def main():
system_arguments = sys.argv[1:]
options = 'd:'
long_options = ['directory=']
try:
arguments, values = getopt.getopt(system_arguments, options, long_options)
for arg, val in arguments:
if arg in ("-d", "--directory"):
folderPath = os.path.join(os.getcwd(), val)
imgConversionCount = convertToPNG(folderPath)
print(f"successfully converted all {imgConversionCount} images from {folderPath} to png")
except getopt.error as error:
print(str(error))
if __name__ == '__main__':
main()