fetchart/artresizer: Improve comments & docstrings, avoid os.stat if

possible

slight enhancements on top of https://github.com/beetbox/beets/pull/3560
This commit is contained in:
wisp3rwind 2021-03-23 11:57:01 +01:00
parent 07b5e69f40
commit af70b75670
2 changed files with 14 additions and 12 deletions

View file

@ -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)]

View file

@ -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