diff --git a/beets/art.py b/beets/art.py index 66eec88a7b..cc23e420cb 100644 --- a/beets/art.py +++ b/beets/art.py @@ -193,7 +193,7 @@ def extract(log, outpath, item): return # Add an extension to the filename. - ext = imghdr.what(None, h=art) + ext = mediafile.image_extension(art) if not ext: log.warning(u'Unknown image type in {0}.', displayable_path(item.path)) diff --git a/beets/mediafile.py b/beets/mediafile.py index 6165875971..3faf2f60ea 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -59,6 +59,7 @@ from beets import logging from beets.util import displayable_path, syspath, as_string +from beets.util.collections import IdentityUnlessDict import six @@ -81,6 +82,8 @@ 'aiff': 'AIFF', } +PREFERRED_IMAGE_EXTENSIONS = IdentityUnlessDict({'jpeg': 'jpg'}) + # Exceptions. @@ -351,6 +354,10 @@ def image_mime_type(data): return 'image/x-{0}'.format(kind) +def image_extension(data): + return PREFERRED_IMAGE_EXTENSIONS[_imghdr_what_wrapper(data)] + + class ImageType(enum.Enum): """Indicates the kind of an `Image` stored in a file's tag. """ diff --git a/beets/util/collections.py b/beets/util/collections.py new file mode 100644 index 0000000000..05ab3a592f --- /dev/null +++ b/beets/util/collections.py @@ -0,0 +1,6 @@ +class IdentityUnlessDict(dict): + def __getitem__(self, key): + try: + return dict.__getitem__(self, key) + except KeyError: + return key