aboutsummaryrefslogtreecommitdiffstats
path: root/bsie/reader/preview/utils.py
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-02-08 19:25:19 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-02-08 19:25:19 +0100
commit7bf6b33fa6d6b901e4933bfe0b2a9939d7b3f3f3 (patch)
treed280d9d1e19e4f7a9d0d4b5405603c729e1fdcce /bsie/reader/preview/utils.py
parent05a841215c82ef40d4679dfc4d2c26572bd4d349 (diff)
parent0d0144466919cfb168e75c2af26d5cb74e10bfa0 (diff)
downloadbsie-7bf6b33fa6d6b901e4933bfe0b2a9939d7b3f3f3.tar.gz
bsie-7bf6b33fa6d6b901e4933bfe0b2a9939d7b3f3f3.tar.bz2
bsie-7bf6b33fa6d6b901e4933bfe0b2a9939d7b3f3f3.zip
Merge branch 'previews' into develop
Diffstat (limited to 'bsie/reader/preview/utils.py')
-rw-r--r--bsie/reader/preview/utils.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/bsie/reader/preview/utils.py b/bsie/reader/preview/utils.py
new file mode 100644
index 0000000..2ef1562
--- /dev/null
+++ b/bsie/reader/preview/utils.py
@@ -0,0 +1,39 @@
+"""
+
+Part of the tagit module.
+A copy of the license is provided with the project.
+Author: Matthias Baumgartner, 2022
+"""
+# standard imports
+import typing
+
+# external imports
+import PIL.Image
+
+# exports
+__all__: typing.Sequence[str] = (
+ 'resize',
+ )
+
+
+## code ##
+
+def resize(
+ img: PIL.Image.Image,
+ max_size: int,
+ ) -> PIL.Image.Image:
+ """Resize an image to a given maximum side length."""
+ # determine target dimensions
+ ratio = img.width / img.height
+ if img.width > img.height:
+ width, height = max_size, round(max_size / ratio)
+ else:
+ width, height = round(ratio * max_size), max_size
+ # rescale and return
+ return img.resize(
+ (width, height),
+ resample=PIL.Image.Resampling.LANCZOS, # create high-quality image
+ reducing_gap=3.0, # optimize computation via fast size reduction
+ )
+
+## EOF ##