stouputils 1.2.13__tar.gz → 1.2.14__tar.gz

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 (25) hide show
  1. {stouputils-1.2.13 → stouputils-1.2.14}/PKG-INFO +1 -1
  2. {stouputils-1.2.13 → stouputils-1.2.14}/pyproject.toml +1 -1
  3. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/__init__.py +1 -0
  4. stouputils-1.2.14/stouputils/image.py +70 -0
  5. {stouputils-1.2.13 → stouputils-1.2.14}/.gitignore +0 -0
  6. {stouputils-1.2.13 → stouputils-1.2.14}/LICENSE +0 -0
  7. {stouputils-1.2.13 → stouputils-1.2.14}/README.md +0 -0
  8. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/all_doctests.py +0 -0
  9. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/applications/__init__.py +0 -0
  10. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/applications/automatic_docs.py +0 -0
  11. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/archive.py +0 -0
  12. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/backup.py +0 -0
  13. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/collections.py +0 -0
  14. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/continuous_delivery/__init__.py +0 -0
  15. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/continuous_delivery/cd_utils.py +0 -0
  16. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/continuous_delivery/github.py +0 -0
  17. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/continuous_delivery/pypi.py +0 -0
  18. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/continuous_delivery/pyproject.py +0 -0
  19. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/ctx.py +0 -0
  20. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/decorators.py +0 -0
  21. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/dont_look/zip_file_override.py +0 -0
  22. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/io.py +0 -0
  23. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/parallel.py +0 -0
  24. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/print.py +0 -0
  25. {stouputils-1.2.13 → stouputils-1.2.14}/stouputils/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: stouputils
3
- Version: 1.2.13
3
+ Version: 1.2.14
4
4
  Summary: Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more.
5
5
  Project-URL: Homepage, https://github.com/Stoupy51/stouputils
6
6
  Project-URL: Issues, https://github.com/Stoupy51/stouputils/issues
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
5
5
 
6
6
  [project]
7
7
  name = "stouputils"
8
- version = "1.2.13"
8
+ version = "1.2.14"
9
9
  description = "Stouputils is a collection of utility modules designed to simplify and enhance the development process. It includes a range of tools for tasks such as execution of doctests, display utilities, decorators, as well as context managers, and many more."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.10"
@@ -25,6 +25,7 @@ from .parallel import *
25
25
  from .all_doctests import *
26
26
  from .collections import *
27
27
  from .backup import *
28
+ from .image import *
28
29
 
29
30
  # Folders
30
31
  from .continuous_delivery import *
@@ -0,0 +1,70 @@
1
+
2
+ # Imports
3
+ from PIL import Image
4
+ from typing import Callable
5
+ from numpy.typing import NDArray
6
+ import numpy as np
7
+
8
+
9
+ # Functions
10
+ def image_resize(
11
+ image: Image.Image | NDArray[np.uint8],
12
+ max_result_size: int,
13
+ resampling: Image.Resampling = Image.Resampling.LANCZOS,
14
+ min_or_max: Callable[[int, int], int] = max,
15
+ return_type: type[Image.Image | NDArray[np.uint8]] = Image.Image,
16
+ ) -> Image.Image | NDArray[np.uint8]:
17
+ """ Resize an image while preserving its aspect ratio.
18
+ Scales the image so that its largest dimension equals max_result_size.
19
+
20
+ Args:
21
+ image (Image.Image | np.ndarray): The image to resize.
22
+ max_result_size (int): Maximum size for the largest dimension.
23
+ resampling (Image.Resampling): PIL resampling filter to use.
24
+ min_or_max (Callable): Function to use to get the minimum or maximum of the two ratios.
25
+ return_type (type): Type of the return value (Image.Image or np.ndarray).
26
+ Returns:
27
+ Image.Image: The resized image with preserved aspect ratio.
28
+ Examples:
29
+ >>> # Test with (height x width x channels) numpy array
30
+ >>> import numpy as np
31
+ >>> array: np.ndarray = np.random.randint(0, 255, (100, 50, 3), dtype=np.uint8)
32
+ >>> image_resize(array, 100).size
33
+ (50, 100)
34
+ >>> image_resize(array, 100, min_or_max=max).size
35
+ (50, 100)
36
+ >>> image_resize(array, 100, min_or_max=min).size
37
+ (100, 200)
38
+
39
+ >>> # Test with PIL Image
40
+ >>> from PIL import Image
41
+ >>> pil_image: Image.Image = Image.new('RGB', (200, 100))
42
+ >>> image_resize(pil_image, 50).size
43
+ (50, 25)
44
+ >>> # Test with different return types
45
+ >>> resized_array = image_resize(array, 50, return_type=np.ndarray)
46
+ >>> isinstance(resized_array, np.ndarray)
47
+ True
48
+ >>> resized_array.shape
49
+ (50, 25, 3)
50
+ >>> # Test with different resampling methods
51
+ >>> image_resize(pil_image, 50, resampling=Image.Resampling.NEAREST).size
52
+ (50, 25)
53
+ """
54
+ if isinstance(image, np.ndarray):
55
+ image = Image.fromarray(image) # type: ignore
56
+
57
+ width: int = image.size[0]
58
+ height: int = image.size[1]
59
+ max_dimension: int = min_or_max(width, height)
60
+ scale: float = max_result_size / max_dimension
61
+
62
+ new_width: int = int(width * scale)
63
+ new_height: int = int(height * scale)
64
+ new_image: Image.Image = image.resize((new_width, new_height), resampling)
65
+
66
+ if return_type == np.ndarray:
67
+ return np.array(new_image)
68
+ else:
69
+ return new_image
70
+
File without changes
File without changes
File without changes