Skip to content

Commit

Permalink
refactor: split imagetools from module to package
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki committed May 9, 2023
1 parent 2fdee3d commit 4c64b71
Show file tree
Hide file tree
Showing 14 changed files with 1,529 additions and 1,404 deletions.
1,034 changes: 0 additions & 1,034 deletions lektor/imagetools.py

This file was deleted.

13 changes: 13 additions & 0 deletions lektor/imagetools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .exif import read_exif
from .image_info import get_image_info
from .thumbnail import make_image_thumbnail
from .thumbnail import Thumbnail
from .thumbnail import ThumbnailMode

__all__ = [
"get_image_info",
"make_image_thumbnail",
"read_exif",
"Thumbnail",
"ThumbnailMode",
]
34 changes: 34 additions & 0 deletions lektor/imagetools/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Compatibility with various versions of Pillow."""
from __future__ import annotations

from enum import IntEnum
from types import ModuleType
from types import SimpleNamespace
from typing import Mapping

import PIL.ExifTags

__all__ = ["ExifTags", "UnidentifiedImageError"]

PILLOW_VERSION_INFO = tuple(map(int, PIL.__version__.split(".")))

if PILLOW_VERSION_INFO >= (9, 4):
ExifTags: ModuleType | SimpleNamespace = PIL.ExifTags
else:
# Pillow < 9.4 does not provide the PIL.ExifTags.{Base,GPS,IFD} enums. Here we provide
# and ExifTags namespace which has them.

def _reverse_map(mapping: Mapping[int, str]) -> dict[str, int]:
return dict(map(reversed, mapping.items())) # type: ignore[arg-type]

ExifTags = SimpleNamespace(
Base=IntEnum("Base", _reverse_map(PIL.ExifTags.TAGS)),
GPS=IntEnum("GPS", _reverse_map(PIL.ExifTags.GPSTAGS)),
IFD=IntEnum("IFD", [("Exif", 34665), ("GPSInfo", 34853)]),
TAGS=PIL.ExifTags.TAGS,
GPSTAGS=PIL.ExifTags.GPSTAGS,
)


# UnidentifiedImageError only exists in Pillow >= 7.0.0
UnidentifiedImageError = getattr(PIL, "UnidentifiedImageError", OSError)

0 comments on commit 4c64b71

Please sign in to comment.