From 2d62032a61677b2a213541b5122a7ed5646cba5b Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Tue, 27 Jan 2015 16:48:20 +0100 Subject: [PATCH] ArtResizer can fetch an image file's size Useful for the thumbnails plugin --- beets/util/artresizer.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/beets/util/artresizer.py b/beets/util/artresizer.py index b1920d8ac..62f700981 100644 --- a/beets/util/artresizer.py +++ b/beets/util/artresizer.py @@ -107,6 +107,35 @@ BACKEND_FUNCS = { } +def pil_getsize(path_in): + from PIL import Image + try: + im = Image.open(util.syspath(path_in)) + return im.size + except IOError: + log.error(u"PIL cannot compute size of '{0}'", + util.displayable_path(path_in)) + + +def im_getsize(path_in): + try: + out = util.command_output(['identify', util.syspath(path_in)]) + except subprocess.CalledProcessError: + log.warn(u'IM cannot compute size of {0}', + util.displayable_path(path_in)) + return + try: + return out.split(' ')[-7].split('x') + except IndexError: + log.warn(u'Could not understand IM output: {0!r}', out) + + +BACKEND_GET_SIZE = { + PIL: pil_getsize, + IMAGEMAGICK: im_getsize, +} + + class Shareable(type): """A pseudo-singleton metaclass that allows both shared and non-shared instances. The ``MyClass.shared`` property holds a @@ -165,6 +194,16 @@ class ArtResizer(object): """ return self.method[0] in BACKEND_FUNCS + def get_size(self, path_in): + """Return the size of an image file as an int couple (width, height) + in pixels. + + Only available locally + """ + if self.local: + func = BACKEND_GET_SIZE[self.method[0]] + return func(path_in) + def _can_compare(self): """A boolean indicating whether image comparison is available"""