stouputils 1.2.13__tar.gz → 1.2.15__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.15}/PKG-INFO +1 -1
  2. {stouputils-1.2.13 → stouputils-1.2.15}/pyproject.toml +1 -1
  3. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/__init__.py +1 -0
  4. stouputils-1.2.15/stouputils/image.py +85 -0
  5. {stouputils-1.2.13 → stouputils-1.2.15}/.gitignore +0 -0
  6. {stouputils-1.2.13 → stouputils-1.2.15}/LICENSE +0 -0
  7. {stouputils-1.2.13 → stouputils-1.2.15}/README.md +0 -0
  8. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/all_doctests.py +0 -0
  9. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/applications/__init__.py +0 -0
  10. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/applications/automatic_docs.py +0 -0
  11. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/archive.py +0 -0
  12. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/backup.py +0 -0
  13. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/collections.py +0 -0
  14. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/continuous_delivery/__init__.py +0 -0
  15. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/continuous_delivery/cd_utils.py +0 -0
  16. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/continuous_delivery/github.py +0 -0
  17. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/continuous_delivery/pypi.py +0 -0
  18. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/continuous_delivery/pyproject.py +0 -0
  19. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/ctx.py +0 -0
  20. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/decorators.py +0 -0
  21. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/dont_look/zip_file_override.py +0 -0
  22. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/io.py +0 -0
  23. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/parallel.py +0 -0
  24. {stouputils-1.2.13 → stouputils-1.2.15}/stouputils/print.py +0 -0
  25. {stouputils-1.2.13 → stouputils-1.2.15}/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.15
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.15"
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,85 @@
1
+
2
+ # Imports
3
+ from PIL import Image
4
+ from typing import Callable, Any
5
+ import numpy as np
6
+
7
+ # Functions
8
+ def image_resize(
9
+ image: Image.Image | np.ndarray[Any, np.dtype[np.uint8]],
10
+ max_result_size: int,
11
+ resampling: Image.Resampling = Image.Resampling.LANCZOS,
12
+ min_or_max: Callable[[int, int], int] = max,
13
+ return_type: type[Image.Image | np.ndarray[Any, np.dtype[np.uint8]]] = Image.Image,
14
+ keep_aspect_ratio: bool = True,
15
+ ) -> Any:
16
+ """ Resize an image while preserving its aspect ratio by default.
17
+ Scales the image so that its largest dimension equals max_result_size.
18
+
19
+ Args:
20
+ image (Image.Image | np.ndarray): The image to resize.
21
+ max_result_size (int): Maximum size for the largest dimension.
22
+ resampling (Image.Resampling): PIL resampling filter to use.
23
+ min_or_max (Callable): Function to use to get the minimum or maximum of the two ratios.
24
+ return_type (type): Type of the return value (Image.Image or np.ndarray).
25
+ keep_aspect_ratio (bool): Whether to keep the aspect ratio.
26
+ Returns:
27
+ Image.Image | np.ndarray[Any, np.dtype[np.uint8]]: 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
+ # Convert numpy array to PIL Image if needed
55
+ if isinstance(image, np.ndarray):
56
+ image = Image.fromarray(image)
57
+
58
+ if keep_aspect_ratio:
59
+
60
+ # Get original image dimensions
61
+ width: int = image.size[0]
62
+ height: int = image.size[1]
63
+
64
+ # Determine which dimension to use for scaling based on min_or_max function
65
+ max_dimension: int = min_or_max(width, height)
66
+
67
+ # Calculate scaling factor
68
+ scale: float = max_result_size / max_dimension
69
+
70
+ # Calculate new dimensions while preserving aspect ratio
71
+ new_width: int = int(width * scale)
72
+ new_height: int = int(height * scale)
73
+
74
+ # Resize the image with the calculated dimensions
75
+ new_image: Image.Image = image.resize((new_width, new_height), resampling)
76
+ else:
77
+ # If not keeping aspect ratio, resize to square with max_result_size
78
+ new_image: Image.Image = image.resize((max_result_size, max_result_size), resampling)
79
+
80
+ # Return the image in the requested format
81
+ if return_type == np.ndarray:
82
+ return np.array(new_image)
83
+ else:
84
+ return new_image
85
+
File without changes
File without changes
File without changes