Added new plugin "Optimize Images"
This commit is contained in:
26
optimize_images/Readme.md
Normal file
26
optimize_images/Readme.md
Normal file
@@ -0,0 +1,26 @@
|
||||
Optimize Images Plugin For Pelican
|
||||
==================================
|
||||
|
||||
This plugin applies lossless compression on JPEG and PNG images, with no
|
||||
effect on image quality. It uses [jpegtran][1] and [OptiPNG][2]. It assumes
|
||||
that both of these tools are installed on system path.
|
||||
|
||||
[1]: http://jpegclub.org/jpegtran/ "jpegtran"
|
||||
[2]: http://optipng.sourceforge.net/ "OptiPNG"
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
To enable, ensure that `optimize_images.py` is put somewhere that is accessible.
|
||||
Then use as follows by adding the following to your settings.py:
|
||||
|
||||
PLUGIN_PATH = 'path/to/pelican-plugins'
|
||||
PLUGINS = ["optimize_images"]
|
||||
|
||||
`PLUGIN_PATH` can be a path relative to your settings file or an absolute path.
|
||||
|
||||
Usage
|
||||
-----
|
||||
The plugin will activate and optimize images upon `finalized` signal of
|
||||
pelican.
|
||||
0
optimize_images/__init__.py
Normal file
0
optimize_images/__init__.py
Normal file
51
optimize_images/optimize_images.py
Normal file
51
optimize_images/optimize_images.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
Optimized images (jpg and png)
|
||||
Assumes that jpegtran and optipng are isntalled on path.
|
||||
http://jpegclub.org/jpegtran/
|
||||
http://optipng.sourceforge.net/
|
||||
Copyright (c) 2012 Irfan Ahmad (http://i.com.pk)
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from subprocess import call
|
||||
|
||||
from pelican import signals
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# A list of file types with their respective commands
|
||||
COMMANDS = [
|
||||
('.jpg', 'jpegtran -copy none -optimize "{filename}" "{filename}"'),
|
||||
('.png', 'optipng "{filename}"'),
|
||||
]
|
||||
|
||||
def optimize_images(pelican):
|
||||
"""
|
||||
Optimized jpg and png images
|
||||
|
||||
:param pelican: The Pelican instance
|
||||
"""
|
||||
for dirpath, _, filenames in os.walk(pelican.settings['OUTPUT_PATH']):
|
||||
for name in filenames:
|
||||
optimize(dirpath, name)
|
||||
|
||||
def optimize(dirpath, filename):
|
||||
"""
|
||||
Check if the name is a type of file that should be optimized.
|
||||
And optimizes it if required.
|
||||
|
||||
:param dirpath: Path of the file to be optimzed
|
||||
:param name: A file name to be optimized
|
||||
"""
|
||||
for extension, command in COMMANDS:
|
||||
if filename.endswith(extension):
|
||||
filepath = os.path.join(dirpath, filename)
|
||||
command = command.format(filename=filepath)
|
||||
call(command, shell=True)
|
||||
|
||||
|
||||
def register():
|
||||
signals.finalized.connect(optimize_images)
|
||||
Reference in New Issue
Block a user