-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageFormatConverter.py
43 lines (40 loc) · 1.23 KB
/
ImageFormatConverter.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
from os import chdir, path, rename, listdir, makedirs
from PIL import Image
dir_path = path.dirname(path.realpath(__file__))
chdir(dir_path)
thesefiles = listdir()
target = ".png"
target_size = 800 # max dimension of image, < 1 means no resize
new = ".jpg"
colorspace = 'RGB' # 'L' is black and white, 'RGB' is color
filename = "" # set to "" to not rename files
newdirname = "Original_"+target[1:].upper()+"s"
newdir = f"./{newdirname}/"
tlen = len(target)
if not newdirname in thesefiles:
makedirs(newdir)
i = 0
for f in thesefiles:
if f[-tlen:] == target:
i += 1
image = Image.open(f)
image = image.convert(colorspace)
if target_size >= 1:
cursize = (image.width, image.height)
maxdim = max(cursize)
scale = target_size/maxdim
if scale < 1:
newdim = (int(i*scale) for i in cursize)
image = image.resize(newdim)
if len(filename):
name = filename + str(i) + new
else:
name = f[:-tlen] + new
if True: #move the originals to the backup folder
m = newdir + f
rename(f, m)
image.save(name)
print("processed", f)
else:
pass
#print(f)