aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/image_processing.py
diff options
context:
space:
mode:
authorLibravatarDererk <dererk@debian.org>2015-11-11 16:34:34 -0300
committerLibravatarDererk <dererk@debian.org>2015-11-11 16:34:34 -0300
commit4e3224c012df9f74f010eb92203520515e8537b9 (patch)
tree19322dc0c595268cb6864f21d7e92fd93cb826e9 /nikola/image_processing.py
parent787b97a4cb24330b36f11297c6d3a7a473a907d0 (diff)
Imported Upstream version 7.7.3upstream/7.7.3
Diffstat (limited to 'nikola/image_processing.py')
-rw-r--r--nikola/image_processing.py54
1 files changed, 50 insertions, 4 deletions
diff --git a/nikola/image_processing.py b/nikola/image_processing.py
index 0ba139f..b6f8215 100644
--- a/nikola/image_processing.py
+++ b/nikola/image_processing.py
@@ -29,6 +29,9 @@
from __future__ import unicode_literals
import datetime
import os
+import lxml
+import re
+import gzip
from nikola import utils
@@ -45,15 +48,14 @@ except ImportError:
class ImageProcessor(object):
-
"""Apply image operations."""
image_ext_list_builtin = ['.jpg', '.png', '.jpeg', '.gif', '.svg', '.bmp', '.tiff']
def resize_image(self, src, dst, max_size, bigger_panoramas=True):
"""Make a copy of the image in the requested size."""
- if not Image:
- utils.copy_file(src, dst)
+ if not Image or os.path.splitext(src)[1] in ['.svg', '.svgz']:
+ self.resize_svg(src, dst, max_size, bigger_panoramas)
return
im = Image.open(src)
w, h = im.size
@@ -90,6 +92,48 @@ class ImageProcessor(object):
else: # Image is small
utils.copy_file(src, dst)
+ def resize_svg(self, src, dst, max_size, bigger_panoramas):
+ """Make a copy of an svg at the requested size."""
+ try:
+ # Resize svg based on viewport hacking.
+ # note that this can also lead to enlarged svgs
+ if src.endswith('.svgz'):
+ with gzip.GzipFile(src) as op:
+ xml = op.read()
+ else:
+ with open(src) as op:
+ xml = op.read()
+ tree = lxml.etree.XML(xml)
+ width = tree.attrib['width']
+ height = tree.attrib['height']
+ w = int(re.search("[0-9]+", width).group(0))
+ h = int(re.search("[0-9]+", height).group(0))
+ # calculate new size preserving aspect ratio.
+ ratio = float(w) / h
+ # Panoramas get larger thumbnails because they look *awful*
+ if bigger_panoramas and w > 2 * h:
+ max_size = max_size * 4
+ if w > h:
+ w = max_size
+ h = max_size / ratio
+ else:
+ w = max_size * ratio
+ h = max_size
+ w = int(w)
+ h = int(h)
+ tree.attrib.pop("width")
+ tree.attrib.pop("height")
+ tree.attrib['viewport'] = "0 0 %ipx %ipx" % (w, h)
+ if dst.endswith('.svgz'):
+ op = gzip.GzipFile(dst, 'w')
+ else:
+ op = open(dst, 'w')
+ op.write(lxml.etree.tostring(tree))
+ op.close()
+ except (KeyError, AttributeError) as e:
+ self.logger.warn("No width/height in %s. Original exception: %s" % (src, e))
+ utils.copy_file(src, dst)
+
def image_date(self, src):
"""Try to figure out the date of the image."""
if src not in self.dates:
@@ -103,8 +147,10 @@ class ImageProcessor(object):
decoded = ExifTags.TAGS.get(tag, tag)
if decoded in ('DateTimeOriginal', 'DateTimeDigitized'):
try:
+ if isinstance(value, tuple):
+ value = value[0]
self.dates[src] = datetime.datetime.strptime(
- value, r'%Y:%m:%d %H:%M:%S')
+ value, '%Y:%m:%d %H:%M:%S')
break
except ValueError: # Invalid EXIF date.
pass