-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgshrink.py
88 lines (71 loc) · 2.83 KB
/
imgshrink.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
82
83
84
85
86
87
#! /usr/bin/env python
# coding: utf-8
import os
import re
import click
import subprocess
def filetype_from_ext_and_header(filepath):
path, filename = os.path.split(filepath)
ext = os.path.splitext(filepath)[1]
with open(filename, 'rb') as f:
head = f.read(6)
if re.match(r'.(J|j)(P|p)(E|e)?(G|g)', ext) and \
re.match(rb'\xff\xd8', head):
return 'jpg'
elif re.match(r'.(P|p)(N|n)(G|g)', ext) and \
re.match(rb'.PNG', head):
return 'png'
elif re.match(r'.(G|g)(I|i)(F|f)', ext) and \
re.match(rb'GIF8(7|9)a', head):
return 'gif'
else:
return 'not supported'
def is_command_exists(cmd):
return 0 == subprocess.getstatusoutput('which {}'.format(cmd))[0]
def jpgopt(filepath, output, silent=False):
cmd = 'jpegrescan'
if not is_command_exists(cmd):
print('{cmd} is not found in PATH.'.format(cmd=cmd))
print('install {cmd} from {url}'.format(cmd=cmd, url='https://github.com/kud/jpegrescan'))
return
# raise FileNotFoundError('{cmd} is not found in PATH\n\n{url}'.format(cmd=cmd, url='https://github.com/kud/jpegrescan'))
if silent:
subprocess.run([cmd, '-q', filepath, output])
else:
print('using jpegrescan: optimize {} -> {}'.format(filepath, output))
subprocess.run([cmd, filepath, output])
def pngopt(filepath, output, silent=False):
cmd = 'pngquant'
if not is_command_exists(cmd):
print('{cmd} is not found in PATH.'.format(cmd=cmd))
print('install {cmd} from {url}'.format(cmd=cmd, url='https://pngquant.org/'))
return
# raise FileNotFoundError('{cmd} is not found in PATH\n\n{url}'.format(cmd=cmd, url='https://pngquant.org/'))
if silent:
subprocess.run([cmd, filepath, '--output', output])
else:
print('using {cmd}: optimize {in_} -> {out}'.format(cmd=cmd, in_=filepath, out=output))
subprocess.run([cmd, filepath, '--output', output])
@click.command()
@click.argument('filepath')
@click.option('--output', '-o', default=None)
@click.option('--silent', '-s', is_flag=True)
@click.option('--force', '-f', is_flag=True)
def optimize(filepath, output, silent, force):
if output == None:
output = os.path.splitext(filepath)[0] + '-opti' + os.path.splitext(filepath)[1]
if force == False and os.path.exists(output):
raise FileExistsError('output file {out} is already exists'.format(out=output))
filetype = filetype_from_ext_and_header(filepath)
if filetype == 'jpg':
jpgopt(filepath, output, silent)
elif filetype == 'png':
pngopt(filepath, output, silent)
elif filetype == 'gif':
print('not supported yed.')
else:
print('check if the file is jpg/png/gif.')
def main():
optimize()
if __name__ == '__main__':
main()