-
Notifications
You must be signed in to change notification settings - Fork 1
/
resizeImages.py
56 lines (37 loc) · 1.37 KB
/
resizeImages.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
55
56
import os
import sys
import PIL
from PIL import Image
def compressMe(filepath, verbose=False):
picture = Image.open(filepath)
myHeight, myWidth = picture.size
getSize = os.path.getsize(filepath)
print("sourceSize", getSize, "bytes", picture.size, end="\n")
if (myWidth // 2 > 84 and myHeight // 2 > 84):
picture = picture.resize(
(myHeight // 2, myWidth // 2), PIL.Image.ANTIALIAS)
destFileName = filepath.split(".")[0]+".png"
picture.save((destFileName),
"png",
optimize=True,
quality=10)
newSize = os.path.getsize(destFileName)
print(newSize, myWidth, myHeight)
while (newSize > 2048 and myWidth // 2 > 84 and myHeight // 2 > 84):
picture = Image.open(destFileName)
myHeight, myWidth = picture.size
picture = picture.resize(
(myHeight // 2, myWidth // 2), Image.ANTIALIAS)
picture.save((destFileName),
"png",
optimize=True,
quality=10)
newSize = os.path.getsize(destFileName)
print("destSize", newSize, " bytes", picture.size, end="\n")
return
file = sys.argv[1]
if('.9.png' not in file):
compressMe(file, True)
print("Done")
else:
print("Not allowed to resize")