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

Refactor: Use Pillow for image info #1138

Merged
merged 19 commits into from Nov 7, 2023

Conversation

dairiki
Copy link
Contributor

@dairiki dairiki commented Apr 22, 2023

This replaces our homegrown code in get_image_info() code, and the exifread-based based code in exif_info() with Pillow-based code.

This eliminates two dependencies (on exifread and filetype) and will make it much easier to support other image formats (e.g. WEBP) going forward.

It also guarantees that get_image_info and make_image_thumbnail will agree on things like image type and what images are thumbnailable.

I've done some rudimentary profiling. Currently, the new Pillow-based version of get_image_info is a bit slower (maybe by 30-50%) than the current one. (Getting the image dimensions is very fast, the slower bit is reading the Exif tags to determine whether height and width need to be swapped.)

Issue(s) Resolved

Fixes #

Related Issues / Links

#1099

Description of Changes

  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)

@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch 2 times, most recently from 160d768 to 5522837 Compare April 23, 2023 18:58
@dairiki dairiki added this to the 3.4 milestone Apr 24, 2023
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch 6 times, most recently from 4e99b6d to 4c64b71 Compare May 9, 2023 15:24
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch from 4c64b71 to 99e01c4 Compare May 11, 2023 03:47
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch 2 times, most recently from f123e16 to e27ff9d Compare June 6, 2023 22:38
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch 2 times, most recently from f015ae8 to 115730d Compare August 1, 2023 16:12
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch from 115730d to 4e0237c Compare November 2, 2023 22:25
Use Pillow to determine image format and dimensions rather than our
own home-rolled code.
Remove deprecated property `ThumbnailMode.label` and classmethod
`ThumbnailMode.from_label`.
The following plugins appears to use internal bits of
`lektor.imagetools`:

- https://github.com/numericube/lektor-minification/
- https://github.com/chaos-bodensee/lektor-image-resize/
- https://github.com/skorokithakis/lektor-thumbnail-generator/

From a perusal of their code, it appears that they'll all be
broken by recent Lektor code changes having to do with moving
from ImageMagick to Pillow.

This doesn't completely address the issues for those plugins,
but, for the sake of backward-compatibility, here we:

- re-export `lektor.imagetools.compute_dimensions` (which was moved to
  `lektor.imagetools.thumbnail`).

- re-implement and deprecated `lektor.imagetools.get_quality`
Instead of a Mock, use a real lektor.context.Context in the image
tests.
I'm not sure why this test ever worked.  Different versions of convert
from color strings to HSV values differently.
- rename to `_get_svg_info` to emphasize it is internal
- raise exception rather than returning "unknown" if SVG is unparseable
- rewind file after _get_svg_info call
Pillow >= 9.1 moves many of the constants that were in the top-level
of the PIL.Image module into IntEnums within the module.  But then it
re-exports the enum members to the module programmatically.
Something has changed recently (I'm not sure whether on the Pillow
side or the pylint side) to make pylint throw `no-member` warnings
when these are accessed as module-level attributes.

For the sake of moving forward, here we use the enums, polyfilling
them if running with an older version of Pillow.
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch from 4e0237c to 2a69b6b Compare November 5, 2023 20:12
@dairiki dairiki force-pushed the refactor.use-pillow-for-image-info branch from 2a69b6b to 3898402 Compare November 6, 2023 21:53
@dairiki dairiki merged commit beee7e0 into lektor:master Nov 7, 2023
15 checks passed
@dairiki dairiki deleted the refactor.use-pillow-for-image-info branch November 7, 2023 20:38
@yagebu
Copy link
Contributor

yagebu commented Nov 11, 2023

Nice one, thanks :)

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.

None yet

2 participants