Skip to content

Commit

Permalink
Merge pull request #1036 from kerobaros/master
Browse files Browse the repository at this point in the history
Re: issue #1028: "embedart: Resize images only once per album
  • Loading branch information
sampsyo committed Oct 28, 2014
2 parents a4fd02a + 3a56077 commit 6d38d9f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions beetsplug/embedart.py
Expand Up @@ -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:
Expand All @@ -126,6 +126,8 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None,
displayable_path(imagepath)
))
return
if maxwidth and not asalbum:
imagepath = resize_image(imagepath, maxwidth)

try:
log.debug(u'embedart: embedding {0}'.format(
Expand All @@ -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,
Expand All @@ -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'].get(bool))
config['embedart']['ifempty'].get(bool), 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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6d38d9f

Please sign in to comment.