From 66941d1acfdd0798d14e2b58c137d26a57f8dd77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Sat, 16 Jan 2021 12:50:40 +0100 Subject: [PATCH] Fix flake8 errors --- moviepy/video/fx/resize.py | 131 +++++++++++++++++++++++++++---------- 1 file changed, 98 insertions(+), 33 deletions(-) diff --git a/moviepy/video/fx/resize.py b/moviepy/video/fx/resize.py index 98c8eda4a..576d9e4f6 100644 --- a/moviepy/video/fx/resize.py +++ b/moviepy/video/fx/resize.py @@ -1,10 +1,8 @@ -resize_possible = True - -try: - # TRY USING OpenCV AS RESIZER - # raise ImportError #debugging - import cv2 - import numpy as np +def _get_cv2_resizer(): + try: + import cv2 + except ImportError: + return (None, ["OpenCV not found (install 'opencv-python')"]) def resizer(pic, new_size): lx, ly = int(new_size[0]), int(new_size[1]) @@ -16,43 +14,105 @@ def resizer(pic, new_size): interpolation = cv2.INTER_AREA return cv2.resize(+pic.astype("uint8"), (lx, ly), interpolation=interpolation) - resizer.origin = "cv2" + return (resizer, []) -except ImportError: +def _get_PIL_resizer(): try: - # TRY USING PIL/PILLOW AS RESIZER from PIL import Image - import numpy as np + except ImportError: + return (None, ["PIL not found (install 'Pillow')"]) - def resizer(pic, new_size): - new_size = list(map(int, new_size))[::-1] - # shape = pic.shape - # if len(shape) == 3: - # newshape = (new_size[0], new_size[1], shape[2]) - # else: - # newshape = (new_size[0], new_size[1]) + import numpy as np - pil_img = Image.fromarray(pic) - resized_pil = pil_img.resize(new_size[::-1], Image.ANTIALIAS) - # arr = np.fromstring(resized_pil.tostring(), dtype='uint8') - # arr.reshape(newshape) - return np.array(resized_pil) + def resizer(pic, new_size): + new_size = list(map(int, new_size))[::-1] + # shape = pic.shape + # if len(shape) == 3: + # newshape = (new_size[0], new_size[1], shape[2]) + # else: + # newshape = (new_size[0], new_size[1]) - resizer.origin = "PIL" + pil_img = Image.fromarray(pic) + resized_pil = pil_img.resize(new_size[::-1], Image.ANTIALIAS) + # arr = np.fromstring(resized_pil.tostring(), dtype="uint8") + # arr.reshape(newshape) + return np.array(resized_pil) + return (resizer, []) + + +def _get_scipy_resizer(): + try: + from scipy.misc import imresize except ImportError: - # TRY USING SCIPY AS RESIZER try: - from scipy.misc import imresize + from scipy import __version__ as __scipy_version__ + except ImportError: + return (None, ["Scipy not found (install 'scipy' or 'Pillow')"]) + + scipy_version_info = tuple( + int(num) for num in __scipy_version__.split(".") if num.isdigit() + ) + + # ``scipy.misc.imresize`` was removed in v1.3.0 + if scipy_version_info >= (1, 3, 0): + return ( + None, + [ + "scipy.misc.imresize not found (was deprecated in scipy v1.3.0," + f" you are using v{__scipy_version__}, install 'Pillow')" + ], + ) + + # unknown reason + return (None, "scipy.misc.imresize not found") - def resizer(pic, new_size): - return imresize(pic, map(int, new_size[::-1])) + def resizer(pic, new_size): + return imresize(pic, map(int, new_size[::-1])) - resizer.origin = "Scipy" + return (resizer, []) - except ImportError: - resize_possible = False + +def _get_resizer(): + """Tries to define a ``resizer`` function using next libraries, in the given + order: + + - cv2 + - PIL + - scipy + + Returns a dictionary with following attributes: + + - ``resizer``: Function used to resize images in ``resize`` FX function. + - ``origin``: Library used to resize. + - ``error_msgs``: If any of the libraries is available, shows the user why + this feature is not available and how to fix it in several error messages + which are formatted in the error displayed, if resizing is not possible. + """ + error_messages = [] + + resizer_getters = { + "cv2": _get_cv2_resizer, + "PIL": _get_PIL_resizer, + "scipy": _get_scipy_resizer, + } + for origin, resizer_getter in resizer_getters.items(): + resizer, _error_messages = resizer_getter() + if resizer is not None: + return {"resizer": resizer, "origin": origin, "error_msgs": []} + else: + error_messages.extend(_error_messages) + + return {"resizer": None, "origin": None, "error_msgs": reversed(error_messages)} + + +resizer = None +_resizer_data = _get_resizer() +if _resizer_data["resizer"] is not None: + resizer = _resizer_data["resizer"] + resizer.origin = _resizer_data["origin"] + del _resizer_data["error_msgs"] def resize(clip, new_size=None, height=None, width=None, apply_to_mask=True): @@ -174,10 +234,15 @@ def image_filter(pic): return new_clip -if not resize_possible: +if resizer is None: + del resizer + doc = resize.__doc__ def resize(clip, new_size=None, height=None, width=None): - raise ImportError("fx resize needs OpenCV or Scipy or PIL") + fix_tips = "- " + "\n- ".join(_resizer_data["error_msgs"]) + raise ImportError(f"fx resize needs OpenCV or Scipy or PIL\n{fix_tips}") resize.__doc__ = doc + +del _resizer_data["origin"], _resizer_data["resizer"]