-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.py
52 lines (38 loc) · 1.35 KB
/
encode.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
import os, subprocess
import sys, re
import string, subprocess
from PIL import Image
from io import BytesIO
current_dir = os.getcwd()
allFiles = []
error = False
for root, _, files in os.walk('.'):
root = root.lstrip('.').strip(os.sep)
for file in files:
if not file.endswith('.jpg') or file.endswith('.png') or file.endswith('.gif'):
continue
file = os.path.join(root, file)
allFiles.append(file)
if error:
print('Errors detected, please resolve before continuing')
sys.exit()
for file in allFiles:
print(file)
filepath = os.path.join(os.getcwd(), file)
picture = Image.open(filepath)
imgInputFile = BytesIO()
picture.save(imgInputFile, 'png')
imgInputFileSize = imgInputFile.tell()
print("old: {}".format(imgInputFileSize))
#print(int.from_bytes(imgInputFile, "big"))
picture.save(file, optimize=True)
newFileSize = os.path.getsize(filepath)
print("new: {}".format(newFileSize))
compare = imgInputFileSize - newFileSize
print("diff: {}".format(compare))
if imgInputFileSize < newFileSize: # for some reason new size is larger?
picture.save(file)
print("!!! compressed file of {} was larger".format(filepath))
elif compare <= 1024:
picture.save(file)
print("--- changes less than 1024b, diff: {}".format(compare))