stouputils 1.14.0__py3-none-any.whl → 1.14.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. stouputils/__init__.pyi +15 -0
  2. stouputils/_deprecated.pyi +12 -0
  3. stouputils/all_doctests.pyi +46 -0
  4. stouputils/applications/__init__.pyi +2 -0
  5. stouputils/applications/automatic_docs.py +3 -0
  6. stouputils/applications/automatic_docs.pyi +106 -0
  7. stouputils/applications/upscaler/__init__.pyi +3 -0
  8. stouputils/applications/upscaler/config.pyi +18 -0
  9. stouputils/applications/upscaler/image.pyi +109 -0
  10. stouputils/applications/upscaler/video.pyi +60 -0
  11. stouputils/archive.pyi +67 -0
  12. stouputils/backup.pyi +109 -0
  13. stouputils/collections.pyi +86 -0
  14. stouputils/continuous_delivery/__init__.pyi +5 -0
  15. stouputils/continuous_delivery/cd_utils.pyi +129 -0
  16. stouputils/continuous_delivery/github.pyi +162 -0
  17. stouputils/continuous_delivery/pypi.pyi +52 -0
  18. stouputils/continuous_delivery/pyproject.pyi +67 -0
  19. stouputils/continuous_delivery/stubs.pyi +39 -0
  20. stouputils/ctx.pyi +211 -0
  21. stouputils/data_science/config/get.py +51 -51
  22. stouputils/data_science/data_processing/image/__init__.py +66 -66
  23. stouputils/data_science/data_processing/image/auto_contrast.py +79 -79
  24. stouputils/data_science/data_processing/image/axis_flip.py +58 -58
  25. stouputils/data_science/data_processing/image/bias_field_correction.py +74 -74
  26. stouputils/data_science/data_processing/image/binary_threshold.py +73 -73
  27. stouputils/data_science/data_processing/image/blur.py +59 -59
  28. stouputils/data_science/data_processing/image/brightness.py +54 -54
  29. stouputils/data_science/data_processing/image/canny.py +110 -110
  30. stouputils/data_science/data_processing/image/clahe.py +92 -92
  31. stouputils/data_science/data_processing/image/common.py +30 -30
  32. stouputils/data_science/data_processing/image/contrast.py +53 -53
  33. stouputils/data_science/data_processing/image/curvature_flow_filter.py +74 -74
  34. stouputils/data_science/data_processing/image/denoise.py +378 -378
  35. stouputils/data_science/data_processing/image/histogram_equalization.py +123 -123
  36. stouputils/data_science/data_processing/image/invert.py +64 -64
  37. stouputils/data_science/data_processing/image/laplacian.py +60 -60
  38. stouputils/data_science/data_processing/image/median_blur.py +52 -52
  39. stouputils/data_science/data_processing/image/noise.py +59 -59
  40. stouputils/data_science/data_processing/image/normalize.py +65 -65
  41. stouputils/data_science/data_processing/image/random_erase.py +66 -66
  42. stouputils/data_science/data_processing/image/resize.py +69 -69
  43. stouputils/data_science/data_processing/image/rotation.py +80 -80
  44. stouputils/data_science/data_processing/image/salt_pepper.py +68 -68
  45. stouputils/data_science/data_processing/image/sharpening.py +55 -55
  46. stouputils/data_science/data_processing/image/shearing.py +64 -64
  47. stouputils/data_science/data_processing/image/threshold.py +64 -64
  48. stouputils/data_science/data_processing/image/translation.py +71 -71
  49. stouputils/data_science/data_processing/image/zoom.py +83 -83
  50. stouputils/data_science/data_processing/image_augmentation.py +118 -118
  51. stouputils/data_science/data_processing/image_preprocess.py +183 -183
  52. stouputils/data_science/data_processing/prosthesis_detection.py +359 -359
  53. stouputils/data_science/data_processing/technique.py +481 -481
  54. stouputils/data_science/dataset/__init__.py +45 -45
  55. stouputils/data_science/dataset/dataset.py +292 -292
  56. stouputils/data_science/dataset/dataset_loader.py +135 -135
  57. stouputils/data_science/dataset/grouping_strategy.py +296 -296
  58. stouputils/data_science/dataset/image_loader.py +100 -100
  59. stouputils/data_science/dataset/xy_tuple.py +696 -696
  60. stouputils/data_science/metric_dictionnary.py +106 -106
  61. stouputils/data_science/mlflow_utils.py +206 -206
  62. stouputils/data_science/models/abstract_model.py +149 -149
  63. stouputils/data_science/models/all.py +85 -85
  64. stouputils/data_science/models/keras/all.py +38 -38
  65. stouputils/data_science/models/keras/convnext.py +62 -62
  66. stouputils/data_science/models/keras/densenet.py +50 -50
  67. stouputils/data_science/models/keras/efficientnet.py +60 -60
  68. stouputils/data_science/models/keras/mobilenet.py +56 -56
  69. stouputils/data_science/models/keras/resnet.py +52 -52
  70. stouputils/data_science/models/keras/squeezenet.py +233 -233
  71. stouputils/data_science/models/keras/vgg.py +42 -42
  72. stouputils/data_science/models/keras/xception.py +38 -38
  73. stouputils/data_science/models/keras_utils/callbacks/__init__.py +20 -20
  74. stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +219 -219
  75. stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +148 -148
  76. stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
  77. stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +249 -249
  78. stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +66 -66
  79. stouputils/data_science/models/keras_utils/losses/__init__.py +12 -12
  80. stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +56 -56
  81. stouputils/data_science/models/keras_utils/visualizations.py +416 -416
  82. stouputils/data_science/models/sandbox.py +116 -116
  83. stouputils/data_science/range_tuple.py +234 -234
  84. stouputils/data_science/utils.py +285 -285
  85. stouputils/decorators.pyi +242 -0
  86. stouputils/image.pyi +172 -0
  87. stouputils/installer/__init__.py +18 -18
  88. stouputils/installer/__init__.pyi +5 -0
  89. stouputils/installer/common.pyi +39 -0
  90. stouputils/installer/downloader.pyi +24 -0
  91. stouputils/installer/linux.py +144 -144
  92. stouputils/installer/linux.pyi +39 -0
  93. stouputils/installer/main.py +223 -223
  94. stouputils/installer/main.pyi +57 -0
  95. stouputils/installer/windows.py +136 -136
  96. stouputils/installer/windows.pyi +31 -0
  97. stouputils/io.pyi +213 -0
  98. stouputils/parallel.py +12 -10
  99. stouputils/parallel.pyi +211 -0
  100. stouputils/print.pyi +136 -0
  101. stouputils/py.typed +1 -1
  102. stouputils/stouputils/parallel.pyi +4 -4
  103. stouputils/version_pkg.pyi +15 -0
  104. {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/METADATA +1 -1
  105. stouputils-1.14.2.dist-info/RECORD +171 -0
  106. stouputils-1.14.0.dist-info/RECORD +0 -140
  107. {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/WHEEL +0 -0
  108. {stouputils-1.14.0.dist-info → stouputils-1.14.2.dist-info}/entry_points.txt +0 -0
@@ -1,59 +1,59 @@
1
-
2
- # Imports
3
- from .common import Any, NDArray, check_image, np
4
-
5
-
6
- # Functions
7
- def noise_image(image: NDArray[Any], amount: float, ignore_dtype: bool = False) -> NDArray[Any]:
8
- """ Add Gaussian noise to an image.
9
-
10
- Args:
11
- image (NDArray[Any]): Image to add noise to
12
- amount (float): Amount of noise to add (between 0 and 1)
13
- ignore_dtype (bool): Ignore the dtype check
14
- Returns:
15
- NDArray[Any]: Noisy image
16
-
17
- >>> ## Basic tests
18
- >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
19
- >>> noisy = noise_image(image.astype(np.uint8), 0.5)
20
- >>> noisy.shape == image.shape
21
- True
22
- >>> bool(np.all(noisy >= 0) and np.all(noisy <= 255))
23
- True
24
-
25
- >>> np.random.seed(0)
26
- >>> image = np.array([[128] * 3] * 3)
27
- >>> noise_image(image.astype(np.uint8), 0.1).tolist()
28
- [[172, 138, 152], [185, 175, 104], [152, 125, 126]]
29
-
30
- >>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
31
- >>> noisy_rgb = noise_image(rgb, 0.1)
32
- >>> noisy_rgb.shape == (3,3,3)
33
- True
34
-
35
- >>> ## Test invalid inputs
36
- >>> noise_image("not an image", 0.5)
37
- Traceback (most recent call last):
38
- ...
39
- AssertionError: Image must be a numpy array
40
-
41
- >>> noise_image(image.astype(np.uint8), "0.5")
42
- Traceback (most recent call last):
43
- ...
44
- AssertionError: amount must be a number, got <class 'str'>
45
-
46
- >>> noise_image(image.astype(np.uint8), 1.5)
47
- Traceback (most recent call last):
48
- ...
49
- AssertionError: amount must be between 0 and 1, got 1.5
50
- """
51
- # Check input data
52
- check_image(image, ignore_dtype=ignore_dtype)
53
- assert isinstance(amount, float | int), f"amount must be a number, got {type(amount)}"
54
- assert 0 <= amount <= 1, f"amount must be between 0 and 1, got {amount}"
55
-
56
- # Generate noise
57
- noise: NDArray[Any] = np.random.normal(0, amount * 255, image.shape).astype(np.int16)
58
- return np.clip(image.astype(np.int16) + noise, 0, 255).astype(image.dtype) # pyright: ignore [reportUnknownMemberType]
59
-
1
+
2
+ # Imports
3
+ from .common import Any, NDArray, check_image, np
4
+
5
+
6
+ # Functions
7
+ def noise_image(image: NDArray[Any], amount: float, ignore_dtype: bool = False) -> NDArray[Any]:
8
+ """ Add Gaussian noise to an image.
9
+
10
+ Args:
11
+ image (NDArray[Any]): Image to add noise to
12
+ amount (float): Amount of noise to add (between 0 and 1)
13
+ ignore_dtype (bool): Ignore the dtype check
14
+ Returns:
15
+ NDArray[Any]: Noisy image
16
+
17
+ >>> ## Basic tests
18
+ >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
19
+ >>> noisy = noise_image(image.astype(np.uint8), 0.5)
20
+ >>> noisy.shape == image.shape
21
+ True
22
+ >>> bool(np.all(noisy >= 0) and np.all(noisy <= 255))
23
+ True
24
+
25
+ >>> np.random.seed(0)
26
+ >>> image = np.array([[128] * 3] * 3)
27
+ >>> noise_image(image.astype(np.uint8), 0.1).tolist()
28
+ [[172, 138, 152], [185, 175, 104], [152, 125, 126]]
29
+
30
+ >>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
31
+ >>> noisy_rgb = noise_image(rgb, 0.1)
32
+ >>> noisy_rgb.shape == (3,3,3)
33
+ True
34
+
35
+ >>> ## Test invalid inputs
36
+ >>> noise_image("not an image", 0.5)
37
+ Traceback (most recent call last):
38
+ ...
39
+ AssertionError: Image must be a numpy array
40
+
41
+ >>> noise_image(image.astype(np.uint8), "0.5")
42
+ Traceback (most recent call last):
43
+ ...
44
+ AssertionError: amount must be a number, got <class 'str'>
45
+
46
+ >>> noise_image(image.astype(np.uint8), 1.5)
47
+ Traceback (most recent call last):
48
+ ...
49
+ AssertionError: amount must be between 0 and 1, got 1.5
50
+ """
51
+ # Check input data
52
+ check_image(image, ignore_dtype=ignore_dtype)
53
+ assert isinstance(amount, float | int), f"amount must be a number, got {type(amount)}"
54
+ assert 0 <= amount <= 1, f"amount must be between 0 and 1, got {amount}"
55
+
56
+ # Generate noise
57
+ noise: NDArray[Any] = np.random.normal(0, amount * 255, image.shape).astype(np.int16)
58
+ return np.clip(image.astype(np.int16) + noise, 0, 255).astype(image.dtype) # pyright: ignore [reportUnknownMemberType]
59
+
@@ -1,65 +1,65 @@
1
-
2
- # pyright: reportUnknownVariableType=false
3
- # pyright: reportUnusedImport=false
4
- # pyright: reportArgumentType=false
5
- # pyright: reportCallIssue=false
6
-
7
- # Imports
8
- from .common import Any, NDArray, check_image, cv2, np # noqa: F401
9
-
10
-
11
- # Functions
12
- def normalize_image(
13
- image: NDArray[Any],
14
- a: float | int = 0,
15
- b: float | int = 255,
16
- method: int = cv2.NORM_MINMAX,
17
- ignore_dtype: bool = False,
18
- ) -> NDArray[Any]:
19
- """ Normalize an image to the range 0-255.
20
-
21
- Args:
22
- image (NDArray[Any]): Image to normalize
23
- a (float | int): Minimum value (default: 0)
24
- b (float | int): Maximum value (default: 255)
25
- method (int): Normalization method (default: cv2.NORM_MINMAX)
26
- ignore_dtype (bool): Ignore the dtype check
27
- Returns:
28
- NDArray[Any]: Normalized image
29
-
30
- >>> ## Basic tests
31
- >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
32
- >>> normalized = normalize_image(image)
33
- >>> normalized.tolist()
34
- [[0, 32, 64], [96, 128, 159], [191, 223, 255]]
35
- >>> normalized.shape == image.shape
36
- True
37
- >>> normalized.dtype == image.dtype
38
- True
39
-
40
- >>> ## Test invalid inputs
41
- >>> normalize_image("not an image")
42
- Traceback (most recent call last):
43
- ...
44
- AssertionError: Image must be a numpy array
45
-
46
- >>> normalize_image(image, a="not an integer")
47
- Traceback (most recent call last):
48
- ...
49
- AssertionError: a must be a float or an integer
50
-
51
- >>> normalize_image(image, b="not an integer")
52
- Traceback (most recent call last):
53
- ...
54
- AssertionError: b must be a float or an integer
55
- """
56
- # Check input data
57
- check_image(image, ignore_dtype=ignore_dtype)
58
- assert isinstance(a, float | int), "a must be a float or an integer"
59
- assert isinstance(b, float | int), "b must be a float or an integer"
60
- assert isinstance(method, int), "method must be an integer"
61
- assert a < b, "a must be less than b"
62
-
63
- # Normalize image
64
- return cv2.normalize(image, None, a, b, method)
65
-
1
+
2
+ # pyright: reportUnknownVariableType=false
3
+ # pyright: reportUnusedImport=false
4
+ # pyright: reportArgumentType=false
5
+ # pyright: reportCallIssue=false
6
+
7
+ # Imports
8
+ from .common import Any, NDArray, check_image, cv2, np # noqa: F401
9
+
10
+
11
+ # Functions
12
+ def normalize_image(
13
+ image: NDArray[Any],
14
+ a: float | int = 0,
15
+ b: float | int = 255,
16
+ method: int = cv2.NORM_MINMAX,
17
+ ignore_dtype: bool = False,
18
+ ) -> NDArray[Any]:
19
+ """ Normalize an image to the range 0-255.
20
+
21
+ Args:
22
+ image (NDArray[Any]): Image to normalize
23
+ a (float | int): Minimum value (default: 0)
24
+ b (float | int): Maximum value (default: 255)
25
+ method (int): Normalization method (default: cv2.NORM_MINMAX)
26
+ ignore_dtype (bool): Ignore the dtype check
27
+ Returns:
28
+ NDArray[Any]: Normalized image
29
+
30
+ >>> ## Basic tests
31
+ >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
32
+ >>> normalized = normalize_image(image)
33
+ >>> normalized.tolist()
34
+ [[0, 32, 64], [96, 128, 159], [191, 223, 255]]
35
+ >>> normalized.shape == image.shape
36
+ True
37
+ >>> normalized.dtype == image.dtype
38
+ True
39
+
40
+ >>> ## Test invalid inputs
41
+ >>> normalize_image("not an image")
42
+ Traceback (most recent call last):
43
+ ...
44
+ AssertionError: Image must be a numpy array
45
+
46
+ >>> normalize_image(image, a="not an integer")
47
+ Traceback (most recent call last):
48
+ ...
49
+ AssertionError: a must be a float or an integer
50
+
51
+ >>> normalize_image(image, b="not an integer")
52
+ Traceback (most recent call last):
53
+ ...
54
+ AssertionError: b must be a float or an integer
55
+ """
56
+ # Check input data
57
+ check_image(image, ignore_dtype=ignore_dtype)
58
+ assert isinstance(a, float | int), "a must be a float or an integer"
59
+ assert isinstance(b, float | int), "b must be a float or an integer"
60
+ assert isinstance(method, int), "method must be an integer"
61
+ assert a < b, "a must be less than b"
62
+
63
+ # Normalize image
64
+ return cv2.normalize(image, None, a, b, method)
65
+
@@ -1,66 +1,66 @@
1
-
2
- # pyright: reportUnknownMemberType=false
3
-
4
- # Imports
5
- from .common import Any, NDArray, check_image, np
6
-
7
-
8
- # Functions
9
- def random_erase_image(image: NDArray[Any], erase_factor: float, ignore_dtype: bool = False) -> NDArray[Any]:
10
- """ Randomly erase a rectangle in the image.
11
-
12
- Args:
13
- image (NDArray[Any]): Image to apply random erase
14
- erase_factor (float): Factor determining the size of the rectangle to erase
15
- ignore_dtype (bool): Ignore the dtype check
16
- Returns:
17
- NDArray[Any]: Image with random erasing applied
18
-
19
- >>> ## Basic tests
20
- >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
21
- >>> erased = random_erase_image(image.astype(np.uint8), 0.5)
22
- >>> erased.shape == image.shape
23
- True
24
-
25
- >>> np.random.seed(42)
26
- >>> img = np.ones((5,5), dtype=np.uint8) * 255
27
- >>> erased = random_erase_image(img, 0.4)
28
- >>> bool(np.any(erased == 0)) # Should have some erased pixels
29
- True
30
-
31
- >>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
32
- >>> erased_rgb = random_erase_image(rgb, 0.3)
33
- >>> erased_rgb.shape == (3,3,3)
34
- True
35
-
36
- >>> ## Test invalid inputs
37
- >>> random_erase_image("not an image", 0.5)
38
- Traceback (most recent call last):
39
- ...
40
- AssertionError: Image must be a numpy array
41
-
42
- >>> random_erase_image(image.astype(np.uint8), "0.5")
43
- Traceback (most recent call last):
44
- ...
45
- AssertionError: erase_factor must be a number, got <class 'str'>
46
- """
47
- # Check input data
48
- check_image(image, ignore_dtype=ignore_dtype)
49
- assert isinstance(erase_factor, float | int), f"erase_factor must be a number, got {type(erase_factor)}"
50
-
51
- # Get image dimensions
52
- height, width = image.shape[:2]
53
-
54
- # Determine size of the rectangle to erase
55
- erase_height: int = int(height * erase_factor)
56
- erase_width: int = int(width * erase_factor)
57
-
58
- # Randomly choose the top-left corner of the rectangle
59
- top_left_x: int = np.random.randint(0, width - erase_width)
60
- top_left_y: int = np.random.randint(0, height - erase_height)
61
-
62
- # Apply random erasing
63
- erased_image: NDArray[Any] = image.copy()
64
- erased_image[top_left_y:top_left_y + erase_height, top_left_x:top_left_x + erase_width] = 0
65
- return erased_image
66
-
1
+
2
+ # pyright: reportUnknownMemberType=false
3
+
4
+ # Imports
5
+ from .common import Any, NDArray, check_image, np
6
+
7
+
8
+ # Functions
9
+ def random_erase_image(image: NDArray[Any], erase_factor: float, ignore_dtype: bool = False) -> NDArray[Any]:
10
+ """ Randomly erase a rectangle in the image.
11
+
12
+ Args:
13
+ image (NDArray[Any]): Image to apply random erase
14
+ erase_factor (float): Factor determining the size of the rectangle to erase
15
+ ignore_dtype (bool): Ignore the dtype check
16
+ Returns:
17
+ NDArray[Any]: Image with random erasing applied
18
+
19
+ >>> ## Basic tests
20
+ >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
21
+ >>> erased = random_erase_image(image.astype(np.uint8), 0.5)
22
+ >>> erased.shape == image.shape
23
+ True
24
+
25
+ >>> np.random.seed(42)
26
+ >>> img = np.ones((5,5), dtype=np.uint8) * 255
27
+ >>> erased = random_erase_image(img, 0.4)
28
+ >>> bool(np.any(erased == 0)) # Should have some erased pixels
29
+ True
30
+
31
+ >>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
32
+ >>> erased_rgb = random_erase_image(rgb, 0.3)
33
+ >>> erased_rgb.shape == (3,3,3)
34
+ True
35
+
36
+ >>> ## Test invalid inputs
37
+ >>> random_erase_image("not an image", 0.5)
38
+ Traceback (most recent call last):
39
+ ...
40
+ AssertionError: Image must be a numpy array
41
+
42
+ >>> random_erase_image(image.astype(np.uint8), "0.5")
43
+ Traceback (most recent call last):
44
+ ...
45
+ AssertionError: erase_factor must be a number, got <class 'str'>
46
+ """
47
+ # Check input data
48
+ check_image(image, ignore_dtype=ignore_dtype)
49
+ assert isinstance(erase_factor, float | int), f"erase_factor must be a number, got {type(erase_factor)}"
50
+
51
+ # Get image dimensions
52
+ height, width = image.shape[:2]
53
+
54
+ # Determine size of the rectangle to erase
55
+ erase_height: int = int(height * erase_factor)
56
+ erase_width: int = int(width * erase_factor)
57
+
58
+ # Randomly choose the top-left corner of the rectangle
59
+ top_left_x: int = np.random.randint(0, width - erase_width)
60
+ top_left_y: int = np.random.randint(0, height - erase_height)
61
+
62
+ # Apply random erasing
63
+ erased_image: NDArray[Any] = image.copy()
64
+ erased_image[top_left_y:top_left_y + erase_height, top_left_x:top_left_x + erase_width] = 0
65
+ return erased_image
66
+
@@ -1,69 +1,69 @@
1
-
2
- # Imports
3
- from PIL import Image
4
-
5
- from .common import Any, NDArray, check_image, np
6
-
7
-
8
- # Functions
9
- def resize_image(
10
- image: NDArray[Any],
11
- width: int,
12
- height: int,
13
- resample: Image.Resampling | int = Image.Resampling.LANCZOS,
14
- ignore_dtype: bool = False
15
- ) -> NDArray[Any]:
16
- """ Resize an image to a new width and height.
17
-
18
- Args:
19
- image (NDArray[Any]): Image to resize
20
- width (int): New width
21
- height (int): New height
22
- resample (Image.Resampling | int): Resampling method
23
- ignore_dtype (bool): Ignore the dtype check
24
- Returns:
25
- NDArray[Any]: Image with resized dimensions
26
-
27
- >>> ## Basic tests
28
- >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
29
- >>> resized = resize_image(image.astype(np.uint8), 6, 6)
30
- >>> resized.shape
31
- (6, 6)
32
-
33
- >>> img = np.ones((5, 5), dtype=np.uint8) * 255
34
- >>> resized = resize_image(img, 10, 10)
35
- >>> resized.shape
36
- (10, 10)
37
-
38
- >>> rgb = np.full((3, 3, 3), 128, dtype=np.uint8)
39
- >>> resized_rgb = resize_image(rgb, 6, 6)
40
- >>> resized_rgb.shape
41
- (6, 6, 3)
42
-
43
- >>> ## Test invalid inputs
44
- >>> resize_image("not an image", 10, 10)
45
- Traceback (most recent call last):
46
- ...
47
- AssertionError: Image must be a numpy array
48
-
49
- >>> resize_image(image.astype(np.uint8), "10", 10)
50
- Traceback (most recent call last):
51
- ...
52
- AssertionError: width must be integer, got <class 'str'>
53
-
54
- >>> resize_image(image.astype(np.uint8), 10, "10")
55
- Traceback (most recent call last):
56
- ...
57
- AssertionError: height must be integer, got <class 'str'>
58
- """
59
- # Check input data
60
- check_image(image, ignore_dtype=ignore_dtype)
61
- assert isinstance(width, int), f"width must be integer, got {type(width)}"
62
- assert isinstance(height, int), f"height must be integer, got {type(height)}"
63
- assert isinstance(resample, Image.Resampling | int), f"resample must be Image.Resampling, got {type(resample)}"
64
-
65
- # Resize image
66
- new_image: Image.Image = Image.fromarray(image)
67
- new_image = new_image.resize((width, height), resample=resample)
68
- return np.array(new_image)
69
-
1
+
2
+ # Imports
3
+ from PIL import Image
4
+
5
+ from .common import Any, NDArray, check_image, np
6
+
7
+
8
+ # Functions
9
+ def resize_image(
10
+ image: NDArray[Any],
11
+ width: int,
12
+ height: int,
13
+ resample: Image.Resampling | int = Image.Resampling.LANCZOS,
14
+ ignore_dtype: bool = False
15
+ ) -> NDArray[Any]:
16
+ """ Resize an image to a new width and height.
17
+
18
+ Args:
19
+ image (NDArray[Any]): Image to resize
20
+ width (int): New width
21
+ height (int): New height
22
+ resample (Image.Resampling | int): Resampling method
23
+ ignore_dtype (bool): Ignore the dtype check
24
+ Returns:
25
+ NDArray[Any]: Image with resized dimensions
26
+
27
+ >>> ## Basic tests
28
+ >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
29
+ >>> resized = resize_image(image.astype(np.uint8), 6, 6)
30
+ >>> resized.shape
31
+ (6, 6)
32
+
33
+ >>> img = np.ones((5, 5), dtype=np.uint8) * 255
34
+ >>> resized = resize_image(img, 10, 10)
35
+ >>> resized.shape
36
+ (10, 10)
37
+
38
+ >>> rgb = np.full((3, 3, 3), 128, dtype=np.uint8)
39
+ >>> resized_rgb = resize_image(rgb, 6, 6)
40
+ >>> resized_rgb.shape
41
+ (6, 6, 3)
42
+
43
+ >>> ## Test invalid inputs
44
+ >>> resize_image("not an image", 10, 10)
45
+ Traceback (most recent call last):
46
+ ...
47
+ AssertionError: Image must be a numpy array
48
+
49
+ >>> resize_image(image.astype(np.uint8), "10", 10)
50
+ Traceback (most recent call last):
51
+ ...
52
+ AssertionError: width must be integer, got <class 'str'>
53
+
54
+ >>> resize_image(image.astype(np.uint8), 10, "10")
55
+ Traceback (most recent call last):
56
+ ...
57
+ AssertionError: height must be integer, got <class 'str'>
58
+ """
59
+ # Check input data
60
+ check_image(image, ignore_dtype=ignore_dtype)
61
+ assert isinstance(width, int), f"width must be integer, got {type(width)}"
62
+ assert isinstance(height, int), f"height must be integer, got {type(height)}"
63
+ assert isinstance(resample, Image.Resampling | int), f"resample must be Image.Resampling, got {type(resample)}"
64
+
65
+ # Resize image
66
+ new_image: Image.Image = Image.fromarray(image)
67
+ new_image = new_image.resize((width, height), resample=resample)
68
+ return np.array(new_image)
69
+