diff --git a/AUTHORS b/AUTHORS index 30d99e0..eca2848 100644 --- a/AUTHORS +++ b/AUTHORS @@ -5,6 +5,7 @@ alphabetical order): - Andreas Sieferlinger - Antoine Pitrou - Christophe-Marie Duquesne +- @franek (François D.) - Giel van Schijndel - Jamie Starke - @jdn06 diff --git a/sigal/gallery.py b/sigal/gallery.py index e0c1cbe..8508219 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -3,6 +3,7 @@ # Copyright (c) 2009-2014 - Simon Conseil # Copyright (c) 2013 - Christophe-Marie Duquesne # Copyright (c) 2014 - Jonas Kaufmann +# Copyright (c) 2015 - François D. # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to @@ -171,12 +172,21 @@ class Image(Media): @cached_property def size(self): try: - return get_size(self) + return get_size(self.dst_path) except (IOError, IndexError, TypeError, AttributeError): self.logger.warning(u'Could not read size %s', - self.src_path) + self.dst_path) return None + @cached_property + def thumb_size(self): + try: + return get_size(self.thumb_path) + except (IOError, IndexError, TypeError, AttributeError): + self.logger.warning(u'Could not read size %s', + self.thumb_path) + return None + class Video(Media): """Gather all informations on a video file.""" diff --git a/sigal/image.py b/sigal/image.py index f0b40f4..5075812 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -1,6 +1,7 @@ # -*- coding:utf-8 -*- # Copyright (c) 2009-2014 - Simon Conseil +# Copyright (c) 2015 - François D. # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to @@ -160,17 +161,19 @@ def process_image(filepath, outpath, settings): return Status.SUCCESS -def get_size(filename): - """Return image size.""" - big = PILImage.open(filename.dst_path) - widthBig,heightBig = big.size - thumb = PILImage.open(filename.thumb_path) - widthThumb,heightThumb = thumb.size - data = { - 'big' : {'width': widthBig, 'height': heightBig}, - 'thumbnail': {'width': widthThumb, 'height': heightThumb}, - } - return data +def get_size(file_path): + """Return image size (width and height).""" + logger = logging.getLogger(__name__) + try: + im = PILImage.open(file_path) + except: + logger.error("Failed to open %s", file_path) + else: + width,height = im.size + return { + 'width': width, + 'height': height + } def get_exif_data(filename): """Return a dict with the raw EXIF data.""" diff --git a/tests/test_image.py b/tests/test_image.py index b6c27b2..1edd9aa 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -6,7 +6,7 @@ from PIL import Image from sigal import init_logging from sigal.image import (generate_image, generate_thumbnail, get_exif_tags, - get_exif_data) + get_exif_data, get_size) from sigal.settings import create_settings CURRENT_DIR = os.path.dirname(__file__) @@ -115,3 +115,13 @@ def test_exif_gps(tmpdir): assert abs(simple['gps']['lat'] - lat) < 0.0001 assert abs(simple['gps']['lon'] - lon) < 0.0001 + +def test_get_size(tmpdir): + """Test reading out image size""" + + test_image = 'flickr_jerquiaga_2394751088_cc-by-nc.jpg' + src_file = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'dir1', 'test1', + test_image) + + result = get_size(src_file) + assert result == {'height': 800, 'width': 600} \ No newline at end of file