From 0865f4a196646d30d313b93c681d1733df9e2b7b Mon Sep 17 00:00:00 2001
From: Tiago Freitas Pereira <tiagofrepereira@gmail.com>
Date: Tue, 26 Apr 2022 18:48:36 +0200
Subject: [PATCH] Replaced the rescale function

---
 bob/pad/face/utils/load_utils.py | 50 ++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/bob/pad/face/utils/load_utils.py b/bob/pad/face/utils/load_utils.py
index 08a4be0e..353df179 100644
--- a/bob/pad/face/utils/load_utils.py
+++ b/bob/pad/face/utils/load_utils.py
@@ -8,6 +8,52 @@ from bob.bio.face.annotator import bounding_box_from_annotation, min_face_size_v
 from bob.bio.video.annotator import normalize_annotations
 from bob.bio.face.color import rgb_to_hsv, rgb_to_yuv
 from imageio import get_reader
+from PIL import Image
+
+
+def scale(image, scaling_factor):
+    """
+    Scales and image using PIL
+
+    Parameters
+    ----------
+
+        image:
+           Input image to be scaled
+
+        new_shape: tuple
+           Shape of the rescaled image
+
+
+
+    """
+
+    if isinstance(scaling_factor, float):
+        new_size = tuple((numpy.array(image.shape) * scaling_factor).astype(numpy.int))
+        return bob.io.image.to_bob(
+            numpy.array(
+                Image.fromarray(bob.io.image.to_matplotlib(image)).resize(
+                    size=new_size
+                ),
+                dtype="float",
+            )
+        )
+
+    elif isinstance(scaling_factor, tuple):
+
+        if len(scaling_factor) > 2:
+            scaling_factor = scaling_factor[1:]
+
+        return bob.io.image.to_bob(
+            numpy.array(
+                Image.fromarray(bob.io.image.to_matplotlib(image)).resize(
+                    size=scaling_factor
+                ),
+                dtype="float",
+            )
+        )
+    else:
+        raise ValueError(f"Scaling factor not supported: {scaling_factor}")
 
 
 def frames(path):
@@ -126,8 +172,8 @@ def scale_face(face, face_height, face_width=None):
     face_width = face_height if face_width is None else face_width
     shape = list(face.shape)
     shape[-2:] = (face_height, face_width)
-    scaled_face = numpy.empty(shape, dtype="float64")
-    scale(face, scaled_face)
+    # scaled_face = numpy.empty(shape, dtype="float64")
+    scaled_face = scale(face, tuple(shape))
     return scaled_face
 
 
-- 
GitLab