From 9a191baca43e65fd00b705304fe89b832731daa3 Mon Sep 17 00:00:00 2001 From: kerobaros Date: Fri, 24 Oct 2014 15:01:52 -0500 Subject: [PATCH] Working on issue #1028, resize images only once per album. --- beetsplug/embedart.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index 1112f6d69..9e1fa986f 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -110,7 +110,7 @@ def album_imported(lib, album): def embed_item(item, imagepath, maxwidth=None, itempath=None, - compare_threshold=0, ifempty=False): + compare_threshold=0, ifempty=False, asalbum=False): """Embed an image into the item's media file. """ if compare_threshold: @@ -126,6 +126,8 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None, displayable_path(imagepath) )) return + if not asalbum: + imagepath = resize_image(imagepath, maxwidth) try: log.debug(u'embedart: embedding {0}'.format( @@ -152,6 +154,8 @@ def embed_album(album, maxwidth=None, quiet=False): log.error(u'Album art not found at {0}' .format(displayable_path(imagepath))) return + if maxwidth: + imagepath = resize_image(imagepath, maxwidth) log.log( logging.DEBUG if quiet else logging.INFO, @@ -161,7 +165,16 @@ def embed_album(album, maxwidth=None, quiet=False): for item in album.items(): embed_item(item, imagepath, maxwidth, None, config['embedart']['compare_threshold'].get(int), - config['embedart']['ifempty']) + config['embedart']['ifempty'], asalbum=True) + + +def resize_image(imagepath, maxwidth): + """Returns path to an image resized to maxwidth. + """ + log.info(u'Resizing album art to {0} pixels wide' + .format(maxwidth)) + imagepath = ArtResizer.shared.resize(maxwidth, syspath(imagepath)) + return imagepath def check_art_similarity(item, imagepath, compare_threshold): @@ -201,11 +214,7 @@ def check_art_similarity(item, imagepath, compare_threshold): def _mediafile_image(image_path, maxwidth=None): """Return a `mediafile.Image` object for the path. - - If maxwidth is set the image is resized if necessary. """ - if maxwidth: - image_path = ArtResizer.shared.resize(maxwidth, syspath(image_path)) with open(syspath(image_path), 'rb') as f: data = f.read()