Skip to content

Commit

Permalink
utils.functions:image_size - use PIL.Image as fallback if imagesize d…
Browse files Browse the repository at this point in the history
…oes not support the file

Fix [#723](/aarhusstadsarkiv/digiarch/issues/723)
  • Loading branch information
MatteoCampinoti94 committed Sep 10, 2024
1 parent d3e6e7f commit 63703aa
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions acacore/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TypeVar

from imagesize import get as get_image_size
from PIL import Image

T = TypeVar("T")
R = TypeVar("R")
Expand Down Expand Up @@ -130,12 +131,18 @@ def image_size(path: Path) -> tuple[int, int]:
"""
Calculate the size of an image.
Supports PNG, JPEG, JPEG2000, GIF, TIFF, SVG, Netpbm, WebP.
Supports PNG, JPEG, JPEG2000, GIF, TIFF, SVG, Netpbm, WebP, BMP.
:param path: The path to the image file.
:raises FileNotFoundError: If the provided path does not exist.
:raises IsADirectoryError: If the provided path points to a directory instead of a file.
:raises ValueError: If the provided file is not a valid image file.
:return: A tuple representing the width and height of the image.
"""
return get_image_size(path)
width, height = get_image_size(path)

if width < 0 or height < 0:
with Image.open(path) as i:
width, height = i.size

return width, height

0 comments on commit 63703aa

Please sign in to comment.