Refactor get_size method and add 2 properties to Image (size and thumb_size).

Add tests for get_size method
This commit is contained in:
franek
2015-06-14 12:54:19 +02:00
parent 20daa3916c
commit a7e9c17489
4 changed files with 38 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ alphabetical order):
- Andreas Sieferlinger - Andreas Sieferlinger
- Antoine Pitrou - Antoine Pitrou
- Christophe-Marie Duquesne - Christophe-Marie Duquesne
- @franek (François D.)
- Giel van Schijndel - Giel van Schijndel
- Jamie Starke - Jamie Starke
- @jdn06 - @jdn06

View File

@@ -3,6 +3,7 @@
# Copyright (c) 2009-2014 - Simon Conseil # Copyright (c) 2009-2014 - Simon Conseil
# Copyright (c) 2013 - Christophe-Marie Duquesne # Copyright (c) 2013 - Christophe-Marie Duquesne
# Copyright (c) 2014 - Jonas Kaufmann # Copyright (c) 2014 - Jonas Kaufmann
# Copyright (c) 2015 - François D.
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to # of this software and associated documentation files (the "Software"), to
@@ -171,12 +172,21 @@ class Image(Media):
@cached_property @cached_property
def size(self): def size(self):
try: try:
return get_size(self) return get_size(self.dst_path)
except (IOError, IndexError, TypeError, AttributeError): except (IOError, IndexError, TypeError, AttributeError):
self.logger.warning(u'Could not read size %s', self.logger.warning(u'Could not read size %s',
self.src_path) self.dst_path)
return None 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): class Video(Media):
"""Gather all informations on a video file.""" """Gather all informations on a video file."""

View File

@@ -1,6 +1,7 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
# Copyright (c) 2009-2014 - Simon Conseil # 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 # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to # of this software and associated documentation files (the "Software"), to
@@ -160,17 +161,19 @@ def process_image(filepath, outpath, settings):
return Status.SUCCESS return Status.SUCCESS
def get_size(filename): def get_size(file_path):
"""Return image size.""" """Return image size (width and height)."""
big = PILImage.open(filename.dst_path) logger = logging.getLogger(__name__)
widthBig,heightBig = big.size try:
thumb = PILImage.open(filename.thumb_path) im = PILImage.open(file_path)
widthThumb,heightThumb = thumb.size except:
data = { logger.error("Failed to open %s", file_path)
'big' : {'width': widthBig, 'height': heightBig}, else:
'thumbnail': {'width': widthThumb, 'height': heightThumb}, width,height = im.size
} return {
return data 'width': width,
'height': height
}
def get_exif_data(filename): def get_exif_data(filename):
"""Return a dict with the raw EXIF data.""" """Return a dict with the raw EXIF data."""

View File

@@ -6,7 +6,7 @@ from PIL import Image
from sigal import init_logging from sigal import init_logging
from sigal.image import (generate_image, generate_thumbnail, get_exif_tags, 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 from sigal.settings import create_settings
CURRENT_DIR = os.path.dirname(__file__) 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']['lat'] - lat) < 0.0001
assert abs(simple['gps']['lon'] - lon) < 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}