From af70b75670b5a887d841bc096c4d75e2c598937f Mon Sep 17 00:00:00 2001 From: wisp3rwind <17089248+wisp3rwind@users.noreply.github.com> Date: Tue, 23 Mar 2021 11:57:01 +0100 Subject: [PATCH] fetchart/artresizer: Improve comments & docstrings, avoid os.stat if possible slight enhancements on top of https://github.com/beetbox/beets/pull/3560 --- beets/util/artresizer.py | 7 +++---- beetsplug/fetchart.py | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/beets/util/artresizer.py b/beets/util/artresizer.py index 0053dd7c0..bf6254c81 100644 --- a/beets/util/artresizer.py +++ b/beets/util/artresizer.py @@ -93,7 +93,7 @@ def pil_resize(maxwidth, path_in, path_out=None, quality=0, max_filesize=0): else: lower_qual = 95 for i in range(5): - # 3 attempts is an abitrary choice + # 5 attempts is an abitrary choice filesize = os.stat(util.syspath(path_out)).st_size log.debug(u"PIL Pass {0} : Output size: {1}B", i, filesize) if filesize <= max_filesize: @@ -133,9 +133,6 @@ def im_resize(maxwidth, path_in, path_out=None, quality=0, max_filesize=0): # "-resize WIDTHx>" shrinks images with the width larger # than the given width while maintaining the aspect ratio # with regards to the height. - - # "-define jpeg:extent=SIZEb" sets the target filesize - # for imagemagick to SIZE in bytes cmd = ArtResizer.shared.im_convert_cmd + [ util.syspath(path_in, prefix=False), '-resize', '{0}x>'.format(maxwidth), @@ -144,6 +141,8 @@ def im_resize(maxwidth, path_in, path_out=None, quality=0, max_filesize=0): if quality > 0: cmd += ['-quality', '{0}'.format(quality)] + # "-define jpeg:extent=SIZEb" sets the target filesize for imagemagick to + # SIZE in bytes. if max_filesize > 0: cmd += ['-define', 'jpeg:extent={0}b'.format(max_filesize)] diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 37db35870..d54e26102 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -73,7 +73,8 @@ class Candidate(object): Return `CANDIDATE_BAD` if the file is unusable. Return `CANDIDATE_EXACT` if the file is usable as-is. Return `CANDIDATE_DOWNSCALE` if the file must be rescaled. - Return `CANDIDATE_DOWNSIZE` if the file must be resized. + Return `CANDIDATE_DOWNSIZE` if the file must be resized, and possibly + also rescaled. """ if not self.path: return self.CANDIDATE_BAD @@ -90,8 +91,9 @@ class Candidate(object): if not self.size: self._log.warning(u'Could not get size of image (please see ' u'documentation for dependencies). ' - u'The configuration options `minwidth` and ' - u'`enforce_ratio` may be violated.') + u'The configuration options `minwidth`, ' + u'`enforce_ratio` and `max_filesize` ' + u'may be violated.') return self.CANDIDATE_EXACT short_edge = min(self.size) @@ -133,12 +135,13 @@ class Candidate(object): downscale = True # Check filesize. - filesize = os.stat(syspath(self.path)).st_size downsize = False - if plugin.max_filesize and filesize > plugin.max_filesize: - self._log.debug(u'image needs resizing ({}B > {}B)', - filesize, plugin.max_filesize) - downsize = True + if plugin.max_filesize: + filesize = os.stat(syspath(self.path)).st_size + if filesize > plugin.max_filesize: + self._log.debug(u'image needs resizing ({}B > {}B)', + filesize, plugin.max_filesize) + downsize = True if downscale: return self.CANDIDATE_DOWNSCALE