From 35147ce46231a309d51b04b97642bc622d976e80 Mon Sep 17 00:00:00 2001 From: "ekaterina.itsenko" Date: Thu, 27 Feb 2025 00:16:02 +0100 Subject: [PATCH] [pycharm] PY-23411 Debugger: add PNG support (except from hover) GitOrigin-RevId: af0bd8bf0be60b06676644941aecb31fec89e25d --- .../tables/images/pydevd_numpy_based_image.py | 10 +++++++++- .../_pydevd_bundle/tables/images/pydevd_numpy_image.py | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_based_image.py b/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_based_image.py index 49df3ef6f60f..d2bab2c44844 100644 --- a/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_based_image.py +++ b/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_based_image.py @@ -18,6 +18,7 @@ DEFAULT_IMAGE_FORMAT = 'PNG' DEFAULT_ENCODING = 'utf-8' GRAYSCALE_MODE = 'L' RGB_MODE = 'RGB' +RGBA_MODE = 'RGBA' def get_bytes(arr): @@ -50,7 +51,14 @@ def get_bytes(arr): else: arr_to_convert = ((arr_to_convert - arr_min) / (arr_max - arr_min) * 255).astype(np.uint8) - mode = GRAYSCALE_MODE if arr_to_convert.ndim == 2 else RGB_MODE + arr_to_convert_ndim = arr_to_convert.ndim + if arr_to_convert_ndim == 2: + mode = GRAYSCALE_MODE + elif arr_to_convert_ndim == 3 and arr_to_convert.shape[2] == 4: + mode = RGBA_MODE + else: + mode = RGB_MODE + bytes_buffer = io.BytesIO() image = Image.fromarray(arr_to_convert, mode=mode) image.save(bytes_buffer, format=DEFAULT_IMAGE_FORMAT) diff --git a/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_image.py b/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_image.py index 30d9c2be6336..1fdc9a45968c 100644 --- a/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_image.py +++ b/python/helpers/pydev/_pydevd_bundle/tables/images/pydevd_numpy_image.py @@ -8,6 +8,7 @@ DEFAULT_IMAGE_FORMAT = 'PNG' DEFAULT_ENCODING = 'utf-8' GRAYSCALE_MODE = 'L' RGB_MODE = 'RGB' +RGBA_MODE = 'RGBA' def get_bytes(arr): @@ -31,7 +32,13 @@ def get_bytes(arr): else: arr_to_convert = ((arr_to_convert - arr_min) / (arr_max - arr_min) * 255).astype(np.uint8) - mode = GRAYSCALE_MODE if arr_to_convert.ndim == 2 else RGB_MODE + arr_to_convert_ndim = arr_to_convert.ndim + if arr_to_convert_ndim == 2: + mode = GRAYSCALE_MODE + elif arr_to_convert_ndim == 3 and arr_to_convert.shape[2] == 4: + mode = RGBA_MODE + else: + mode = RGB_MODE bytes_buffer = io.BytesIO() image = Image.fromarray(arr_to_convert, mode=mode) image.save(bytes_buffer, format=DEFAULT_IMAGE_FORMAT)