Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated imghdr package with filetype #75

Merged
merged 3 commits into from
Nov 3, 2024

Conversation

geigerzaehler
Copy link
Contributor

The imghdr package from the Python standard library is deprecated and will be removed in Python 3.13. The deprecated PEP suggests packages filetype, puremagic, python-magic as replacements. I chose filetype because it has a larger user-base than puremagic and does not have a C dependency, like python-magic.

mediafile.py Outdated


def image_extension(data):
ext = _imghdr_what_wrapper(data)
ext = filetype.guess_extension(data)
return PREFERRED_IMAGE_EXTENSIONS.get(ext, ext)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is PREFERRED_IMAGE_EXTENSIONS still needed? filetype already returns jpg for JPEG files:

$ python3 -c 'import filetype; print(filetype.guess("./only-magic-bytes.jpg").extension)'
jpg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right! Good catch

The `imghdr` package from the Python standard library is [deprecated and
will be removed in Python 3.13][1]. The deprecated PEP suggests packages
filetype, puremagic, python-magic as replacements. I chose filetype
because it has a [larger user-base][2] than puremagic and does not have
a C dependency, like python-magic.

[1]: https://peps.python.org/pep-0594/#deprecated-modules
[2]: https://libraries.io/pypi/filetype
@jackwilsdon
Copy link
Member

It seems like there are quite a few changes to the return values of image_mime_type and image_extension as a part of switching to filetype (images from https://github.com/python/cpython/tree/v3.12.7/Lib/test/imghdrdata):

File Before After Match
imghdrdata/python.exr image/x-exr (exr) ✖️
imghdrdata/python.png image/png (png) image/png (png) ✔️
imghdrdata/python.xbm image/x-xbitmap (xbm) ✖️
imghdrdata/python.sgi image/x-rgb (rgb) ✖️
imghdrdata/python.ras image/x-rast (rast) ✖️
imghdrdata/python.jpg image/jpeg (jpg) image/jpeg (jpg) ✔️
imghdrdata/python.pgm image/x-portable-graymap (pgm) ✖️
imghdrdata/python.webp image/x-webp (webp) image/webp (webp) ✖️
imghdrdata/python.gif image/gif (gif) image/gif (gif) ✔️
imghdrdata/python.bmp image/bmp (bmp) image/bmp (bmp) ✔️
imghdrdata/python.pbm image/x-portable-bitmap (pbm) ✖️
imghdrdata/python.tiff image/tiff (tiff) image/tiff (tif) ✖️
imghdrdata/python-raw.jpg image/jpeg (jpg) image/jpeg (jpg) ✔️
imghdrdata/python.ppm image/x-portable-pixmap (ppm) ✖️

The majority of unrecognized types are ones that are less likely to be used, but it seems like the file extension for tif has changed which isn't ideal. Maybe this needs releasing as a breaking change?

@theol0403
Copy link

For those looking to run beets in python3.13, the following command is a good workaround until this gets merged:

pip install git+https://github.com/geigerzaehler/mediafile/@replace-imghdr

@gbcox
Copy link

gbcox commented Nov 3, 2024

This has been patched in Fedora 41.

  • Apply patches from v0.12.0 to 6accdc4
  • Also apply PR 73 to avoid python 3.13 breakage (although it will regress on more obscure image formats)

@jackwilsdon
Copy link
Member

I've added a change to preserve the tiff extension for TIFF files, resulting in this matchup:

File Before After Match
imghdrdata/python.exr image/x-exr (exr) ✖️
imghdrdata/python.png image/png (png) image/png (png) ✔️
imghdrdata/python.xbm image/x-xbitmap (xbm) ✖️
imghdrdata/python.sgi image/x-rgb (rgb) ✖️
imghdrdata/python.ras image/x-rast (rast) ✖️
imghdrdata/python.jpg image/jpeg (jpg) image/jpeg (jpg) ✔️
imghdrdata/python.pgm image/x-portable-graymap (pgm) ✖️
imghdrdata/python.webp image/x-webp (webp) image/webp (webp) ✖️
imghdrdata/python.gif image/gif (gif) image/gif (gif) ✔️
imghdrdata/python.bmp image/bmp (bmp) image/bmp (bmp) ✔️
imghdrdata/python.pbm image/x-portable-bitmap (pbm) ✖️
imghdrdata/python.tiff image/tiff (tiff) image/tiff (tiff) ✔️
imghdrdata/python-raw.jpg image/jpeg (jpg) image/jpeg (jpg) ✔️
imghdrdata/python.ppm image/x-portable-pixmap (ppm) ✖️

I think this is good enough - the only mismatch is image/x-webp which isn't the correct MIME type for WebP anyway, and the formats we've lost support for aren't normally seen in media files to begin with.

@jackwilsdon
Copy link
Member

Thanks @geigerzaehler!

@jackwilsdon jackwilsdon merged commit e1de364 into beetbox:master Nov 3, 2024
9 checks passed
@geigerzaehler geigerzaehler deleted the replace-imghdr branch November 3, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants