-
Notifications
You must be signed in to change notification settings - Fork 0
/
pelican_image_optimizer.py
102 lines (82 loc) · 3.03 KB
/
pelican_image_optimizer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
Optimized images (gif, jpeg & png)
Assumes that Gifsicle, ImageMagick's convert and pngquant are isntalled on path
http://www.lcdf.org/gifsicle/
http://www.imagemagick.org/
http://pngquant.org/
Copyright (c) 2014 Marc Alexandre (http://www.malexandre.fr)
"""
import os
import re
import shutil
from subprocess import call
from pelican import signals
# The commands list per file type
JPEG = 'convert -strip -interlace Plane -gaussian-blur 0.05 ' +\
'-quality 85% "{filename}" "{filename}."'
COMMANDS = {
'.jpg': JPEG,
'.jpeg': JPEG,
'.png': 'pngquant -o "{filename}." "{filename}"',
'.gif': 'gifsicle --no-warnings -O "{filename}" -o "{filename}."'
}
OPTIMIZED = '_optimized'
FLAG = 'IMAGE_OPTIMIZATION_ONCE_AND_FOR_ALL'
def image_optimizer_initialized(pelican):
"""
Optimized gif, jpg and png images.
@param pelican: The Pelican instance
"""
if not pelican.settings[FLAG]:
return
for dirpath, _, filenames in os.walk(pelican.settings['PATH']):
for name in filenames:
if os.path.splitext(name)[1] in COMMANDS.keys():
if not re.search(OPTIMIZED + r'\.(png|jpg|jpeg|gif)', name):
optimize(pelican, dirpath, name)
def image_optimizer_finalized(pelican):
"""
Optimized gif, jpg and png images. If the
FLAG settings is set to True, just rename
the file alread optimized.
@param pelican: The Pelican instance
"""
for dirpath, _, filenames in os.walk(pelican.settings['OUTPUT_PATH']):
for name in filenames:
if os.path.splitext(name)[1] in COMMANDS.keys():
if pelican.settings[FLAG]:
if '_optimized' in name:
filepath = os.path.join(dirpath, name)
newname = re.sub(OPTIMIZED + r'\.(png|jpg|jpeg|gif)',
r'.\1', name)
newfilepath = os.path.join(dirpath, newname)
shutil.move(filepath, newfilepath)
else:
optimize(pelican, dirpath, name)
def optimize(pelican, dirpath, filename):
"""
Optimize the image.
@param dirpath: Path of folder containing the file to optimze
@param filename: File name to optimize
"""
filepath = os.path.join(dirpath, filename)
ext = os.path.splitext(filename)[1]
command = COMMANDS[ext].format(filename=filepath)
call(command, shell=True)
originsize = os.path.getsize(filepath)
newsize = os.path.getsize(filepath + '.')
if newsize < originsize:
shutil.move(filepath + '.', filepath)
else:
os.remove(filepath + '.')
if pelican.settings[FLAG]:
new_name = re.sub(r'\.(png|jpg|jpeg|gif)',
OPTIMIZED + r'.\1', filename)
shutil.move(filepath, os.path.join(dirpath, new_name))
def register():
"""
Register the plugin in Pelican.
"""
signals.initialized.connect(image_optimizer_initialized)
signals.finalized.connect(image_optimizer_finalized)