diff libervia/backend/tools/image.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
line wrap: on
line diff
--- a/libervia/backend/tools/image.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/tools/image.py	Wed Jun 19 18:44:57 2024 +0200
@@ -32,8 +32,7 @@
 try:
     import cairosvg
 except Exception as e:
-    log.warning(_("SVG support not available, please install cairosvg: {e}").format(
-        e=e))
+    log.warning(_("SVG support not available, please install cairosvg: {e}").format(e=e))
     cairosvg = None
 
 
@@ -55,16 +54,19 @@
     if max_size is None:
         max_size = tuple(host.memory.config_get(None, "image_max", (1200, 720)))
     if image.size > max_size:
-        report['too_large'] = True
+        report["too_large"] = True
         if image.size[0] > max_size[0]:
             factor = max_size[0] / image.size[0]
             if image.size[1] * factor > max_size[1]:
                 factor = max_size[1] / image.size[1]
         else:
             factor = max_size[1] / image.size[1]
-        report['recommended_size'] = [int(image.width*factor), int(image.height*factor)]
+        report["recommended_size"] = [
+            int(image.width * factor),
+            int(image.height * factor),
+        ]
     else:
-        report['too_large'] = False
+        report["too_large"] = False
 
     return report
 
@@ -79,7 +81,7 @@
     if dest is None:
         dest = tempfile.NamedTemporaryFile(suffix=im_path.suffix, delete=False)
     elif isinstance(dest, Path):
-        dest = dest.open('wb')
+        dest = dest.open("wb")
 
     with dest as f:
         resized.save(f, format=im.format)
@@ -102,7 +104,8 @@
         The image at this path should be deleted after use
     """
     return threads.deferToThread(
-        _resize_blocking, image_path, new_size, dest, fix_orientation)
+        _resize_blocking, image_path, new_size, dest, fix_orientation
+    )
 
 
 def _convert_blocking(image_path, dest, extra):
@@ -123,25 +126,26 @@
             try:
                 filepath = Path(name)
             except TypeError:
-                filepath = Path('noname.png')
+                filepath = Path("noname.png")
         else:
-            filepath = Path('noname.png')
+            filepath = Path("noname.png")
 
     if media_type == "image/svg+xml":
         if cairosvg is None:
             raise exceptions.MissingModule(
-                f"Can't convert SVG image at {image_path} due to missing CairoSVG module")
-        width, height = extra.get('width'), extra.get('height')
+                f"Can't convert SVG image at {image_path} due to missing CairoSVG module"
+            )
+        width, height = extra.get("width"), extra.get("height")
         cairosvg.svg2png(
-            url=str(image_path), write_to=dest,
-            output_width=width, output_height=height
+            url=str(image_path), write_to=dest, output_width=width, output_height=height
         )
     else:
         suffix = filepath.suffix
         if not suffix:
             raise ValueError(
                 "A suffix is missing for destination, it is needed to determine file "
-                "format")
+                "format"
+            )
         if not suffix in Image.EXTENSION:
             Image.init()
         try:
@@ -185,7 +189,7 @@
     im_format = im.format
     exif = im.getexif()
     orientation = exif.get(0x0112)
-    if orientation is None or orientation<2:
+    if orientation is None or orientation < 2:
         # nothing to do
         return False
     im = ImageOps.exif_transpose(im)
@@ -218,7 +222,7 @@
         if media_type is not None:
             return media_type
 
-    # file name is not enough, we try to open it
+    # file name is not enough, we try to open it
     img = Image.open(source)
     try:
         return Image.MIME[img.format]