-
Notifications
You must be signed in to change notification settings - Fork 0
/
onefad_squash.py
81 lines (57 loc) · 2.12 KB
/
onefad_squash.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from onefad import *
import shutil, os
from PIL import Image
from io import BytesIO
load_global('cfg', 'grabpostoptions.json')
smb = cfg.get('squash_server')
if smb:
try:
os.listdir(smb)
except FileNotFoundError:
logging.warning('!!!!Cannot connect to remote!!!!')
smb = ''
if not smb:
logging.debug('Squash server path not set')
def prepsys(fol, file):
if not smb:
return False
rext = file.split('.')[-1]
do, fol, file = what_to_do_with(fol, file, rext)
if do in ['copy', 'compress']:
shutil.copyfile(fol + file, smb + 'add/' + file)
if fol == 'ncomp/':
os.remove(fol + file)
return True
return False
def what_to_do_with(fol, file, rext):
if rext.lower() in ['swf', 'mid', 'txt']:
return 'copy', fol, file
if rext.lower() in ['png', 'jpg', 'jpeg', 'bmp', 'jfif']:
src_size = os.path.getsize(fol + file)
if src_size < 51200:# fuke us snakk, it'll do
return 'copy', fol, file
try:
img = Image.open(fol + file)
except Exception as e:
print(e)
return 'copy', fol, file
if img.mode == 'RGBA':
po = Image.new('RGBA', img.size, color='#303030')
img = Image.alpha_composite(po, img)
try:
img = img.convert('RGB')
except OSError as e:
raise Exception('Something went terribly wrong!\nFile: {}\n{}'.format(file, e))
ratio = img.size[1] / img.size[0]
if img.size[0] > 2000 and img.size[1] > 2000:
if ratio > 1:# tall >1
img = img.resize((2000, int(2000*ratio)), Image.ANTIALIAS)
else:# wide <1
img = img.resize((int(2000/ratio), 2000), Image.ANTIALIAS)
test_file = BytesIO()
img.save(test_file, 'jpeg')
if test_file.tell() < src_size:
file = file.replace(rext, 'jpg')
img.save('ncomp/' + file)
return 'compress', 'ncomp/', file
return 'copy', fol, file