pyimagecuda 0.0.8__cp313-cp313-win_amd64.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.
pyimagecuda/fill.py ADDED
@@ -0,0 +1,263 @@
1
+ from typing import Literal
2
+ from .image import Image
3
+ from .pyimagecuda_internal import (fill_color_f32, #type: ignore
4
+ fill_gradient_f32,
5
+ fill_circle_f32,
6
+ fill_checkerboard_f32,
7
+ fill_grid_f32,
8
+ fill_stripes_f32,
9
+ fill_dots_f32,
10
+ fill_noise_f32,
11
+ fill_perlin_f32,
12
+ fill_ngon_f32
13
+ )
14
+
15
+
16
+ class Fill:
17
+
18
+ @staticmethod
19
+ def color(image: Image, rgba: tuple[float, float, float, float]) -> None:
20
+ """
21
+ Fills the image with a solid color (in-place).
22
+
23
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#solid-colors
24
+ """
25
+ fill_color_f32(image._buffer._handle, rgba, image.width, image.height)
26
+
27
+ @staticmethod
28
+ def gradient(image: Image,
29
+ rgba1: tuple[float, float, float, float],
30
+ rgba2: tuple[float, float, float, float],
31
+ direction: Literal['horizontal', 'vertical', 'diagonal', 'radial'] = 'horizontal',
32
+ seamless: bool = False) -> None:
33
+ """
34
+ Fills the image with a gradient (in-place).
35
+
36
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#gradients
37
+ """
38
+ direction_map = {
39
+ 'horizontal': 0,
40
+ 'vertical': 1,
41
+ 'diagonal': 2,
42
+ 'radial': 3
43
+ }
44
+
45
+ dir_int = direction_map.get(direction)
46
+ if dir_int is None:
47
+ raise ValueError(f"Invalid direction: {direction}. Must be one of {list(direction_map.keys())}")
48
+
49
+ fill_gradient_f32(
50
+ image._buffer._handle,
51
+ rgba1,
52
+ rgba2,
53
+ image.width,
54
+ image.height,
55
+ dir_int,
56
+ seamless
57
+ )
58
+
59
+ @staticmethod
60
+ def checkerboard(
61
+ image: Image,
62
+ size: int = 20,
63
+ color1: tuple[float, float, float, float] = (0.8, 0.8, 0.8, 1.0),
64
+ color2: tuple[float, float, float, float] = (0.5, 0.5, 0.5, 1.0),
65
+ offset_x: int = 0,
66
+ offset_y: int = 0
67
+ ) -> None:
68
+ """
69
+ Fills buffer with a checkerboard pattern.
70
+
71
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#checkerboard
72
+ """
73
+ if size <= 0:
74
+ raise ValueError("Checkerboard size must be positive")
75
+
76
+ fill_checkerboard_f32(
77
+ image._buffer._handle,
78
+ image.width, image.height,
79
+ int(size),
80
+ int(offset_x), int(offset_y),
81
+ color1, color2
82
+ )
83
+
84
+ @staticmethod
85
+ def grid(
86
+ image: Image,
87
+ spacing: int = 50,
88
+ line_width: int = 1,
89
+ color: tuple[float, float, float, float] = (0.5, 0.5, 0.5, 1.0),
90
+ bg_color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
91
+ offset_x: int = 0,
92
+ offset_y: int = 0
93
+ ) -> None:
94
+ """
95
+ Fills buffer with a grid pattern.
96
+
97
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#grid
98
+ """
99
+ if spacing <= 0:
100
+ raise ValueError("Grid spacing must be positive")
101
+ if line_width <= 0:
102
+ raise ValueError("Line width must be positive")
103
+
104
+ fill_grid_f32(
105
+ image._buffer._handle,
106
+ image.width, image.height,
107
+ int(spacing), int(line_width),
108
+ int(offset_x), int(offset_y),
109
+ color, bg_color
110
+ )
111
+
112
+ @staticmethod
113
+ def stripes(
114
+ image: Image,
115
+ angle: float = 45.0,
116
+ spacing: int = 40,
117
+ width: int = 20,
118
+ color1: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0),
119
+ color2: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
120
+ offset: int = 0
121
+ ) -> None:
122
+ """
123
+ Fills buffer with alternating stripes with Anti-Aliasing.
124
+
125
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#stripes
126
+ """
127
+ if spacing <= 0:
128
+ raise ValueError("Stripes spacing must be positive")
129
+
130
+ fill_stripes_f32(
131
+ image._buffer._handle,
132
+ image.width, image.height,
133
+ float(angle), int(spacing), int(width), int(offset),
134
+ color1, color2
135
+ )
136
+
137
+ @staticmethod
138
+ def dots(
139
+ image: Image,
140
+ spacing: int = 40,
141
+ radius: float = 10.0,
142
+ color: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0),
143
+ bg_color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
144
+ offset_x: int = 0,
145
+ offset_y: int = 0,
146
+ softness: float = 0.0
147
+ ) -> None:
148
+ """
149
+ Fills buffer with a Polka Dot pattern.
150
+ - softness: 0.0 = Hard edge, 1.0 = Soft glow.
151
+
152
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#dots
153
+ """
154
+ if spacing <= 0:
155
+ raise ValueError("Spacing must be positive")
156
+
157
+ fill_dots_f32(
158
+ image._buffer._handle,
159
+ image.width, image.height,
160
+ int(spacing), float(radius),
161
+ int(offset_x), int(offset_y), float(softness),
162
+ color, bg_color
163
+ )
164
+
165
+ @staticmethod
166
+ def circle(
167
+ image: Image,
168
+ color: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0),
169
+ bg_color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
170
+ softness: float = 0.0
171
+ ) -> None:
172
+ """
173
+ Fills the buffer with a centered circle fitted to the image size.
174
+ - softness: Edge softness. 0.0 = Hard edge (with AA), >0.0 = Soft gradient.
175
+
176
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#circle
177
+ """
178
+
179
+ fill_circle_f32(
180
+ image._buffer._handle,
181
+ image.width, image.height,
182
+ float(softness),
183
+ color, bg_color
184
+ )
185
+
186
+ @staticmethod
187
+ def noise(
188
+ image: Image,
189
+ seed: float = 0.0,
190
+ monochrome: bool = True
191
+ ) -> None:
192
+ """
193
+ Fills the buffer with random White Noise.
194
+ - seed: Random seed. Change this to animate the noise.
195
+ - monochrome: True for grayscale noise, False for RGB noise.
196
+
197
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#noise
198
+ """
199
+
200
+ fill_noise_f32(
201
+ image._buffer._handle,
202
+ image.width, image.height,
203
+ float(seed),
204
+ int(monochrome)
205
+ )
206
+
207
+ @staticmethod
208
+ def perlin(
209
+ image: Image,
210
+ scale: float = 50.0,
211
+ seed: float = 0.0,
212
+ octaves: int = 1,
213
+ persistence: float = 0.5,
214
+ lacunarity: float = 2.0,
215
+ offset_x: float = 0.0,
216
+ offset_y: float = 0.0,
217
+ color1: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 1.0),
218
+ color2: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0)
219
+ ) -> None:
220
+ """
221
+ Fills buffer with Perlin Noise (Gradient Noise).
222
+ - scale: "Zoom" level. Higher values = bigger features (zoomed in).
223
+ - octaves: Detail layers. 1 = smooth, 6 = rocky/detailed.
224
+ - persistence: How much each octave contributes (0.0 to 1.0).
225
+ - lacunarity: Detail frequency multiplier (usually 2.0).
226
+
227
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#perlin-noise
228
+ """
229
+ if scale <= 0: scale = 0.001
230
+
231
+ fill_perlin_f32(
232
+ image._buffer._handle,
233
+ image.width, image.height,
234
+ float(scale), float(seed),
235
+ int(octaves), float(persistence), float(lacunarity),
236
+ float(offset_x), float(offset_y),
237
+ color1, color2
238
+ )
239
+
240
+ @staticmethod
241
+ def ngon(
242
+ image: Image,
243
+ sides: int = 3,
244
+ color: tuple[float, float, float, float] = (1.0, 1.0, 1.0, 1.0),
245
+ bg_color: tuple[float, float, float, float] = (0.0, 0.0, 0.0, 0.0),
246
+ rotation: float = 0.0,
247
+ softness: float = 0.0
248
+ ) -> None:
249
+ """
250
+ Fills buffer with a Regular Polygon (Triangle, Pentagon, Hexagon...).
251
+ - softness: Edge softness (0.0 = Hard AA, >0.0 = Glow).
252
+
253
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/fill/#ngon
254
+ """
255
+ if sides < 3:
256
+ raise ValueError("Polygon must have at least 3 sides")
257
+
258
+ fill_ngon_f32(
259
+ image._buffer._handle,
260
+ image.width, image.height,
261
+ int(sides), float(rotation), float(softness),
262
+ color, bg_color
263
+ )
pyimagecuda/filter.py ADDED
@@ -0,0 +1,198 @@
1
+ from .image import Image
2
+ from .utils import ensure_capacity
3
+ from .io import copy
4
+ from .pyimagecuda_internal import (gaussian_blur_separable_f32, #type: ignore
5
+ sharpen_f32,
6
+ sepia_f32,
7
+ invert_f32,
8
+ threshold_f32,
9
+ solarize_f32,
10
+ filter_sobel_f32,
11
+ filter_emboss_f32)
12
+
13
+
14
+ class Filter:
15
+
16
+ @staticmethod
17
+ def gaussian_blur(
18
+ src: Image,
19
+ radius: int = 3,
20
+ sigma: float | None = None,
21
+ dst_buffer: Image | None = None,
22
+ temp_buffer: Image | None = None
23
+ ) -> Image | None:
24
+ """
25
+ Applies a Gaussian blur to the image (returns new image or writes to buffer).
26
+
27
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#gaussian-blur
28
+ """
29
+
30
+ if radius == 0 or (sigma is not None and sigma <= 0.001):
31
+ if dst_buffer is None:
32
+ dst_buffer = Image(src.width, src.height)
33
+ copy(dst_buffer, src)
34
+ return dst_buffer
35
+ else:
36
+ copy(dst_buffer, src)
37
+ return None
38
+
39
+ if sigma is None:
40
+ sigma = radius / 3.0
41
+
42
+ if dst_buffer is None:
43
+ dst_buffer = Image(src.width, src.height)
44
+ return_dst = True
45
+ else:
46
+ ensure_capacity(dst_buffer, src.width, src.height)
47
+ return_dst = False
48
+
49
+ if temp_buffer is None:
50
+ temp_buffer = Image(src.width, src.height)
51
+ owns_temp = True
52
+ else:
53
+ ensure_capacity(temp_buffer, src.width, src.height)
54
+ owns_temp = False
55
+
56
+ gaussian_blur_separable_f32(
57
+ src._buffer._handle,
58
+ temp_buffer._buffer._handle,
59
+ dst_buffer._buffer._handle,
60
+ src.width,
61
+ src.height,
62
+ radius,
63
+ float(sigma)
64
+ )
65
+
66
+ if owns_temp:
67
+ temp_buffer.free()
68
+
69
+ return dst_buffer if return_dst else None
70
+
71
+ @staticmethod
72
+ def sharpen(
73
+ src: Image,
74
+ strength: float = 1.0,
75
+ dst_buffer: Image | None = None
76
+ ) -> Image | None:
77
+ """
78
+ Sharpens the image (returns new image or writes to buffer).
79
+
80
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#sharpen
81
+ """
82
+
83
+ if abs(strength) < 1e-6:
84
+ if dst_buffer is None:
85
+ dst_buffer = Image(src.width, src.height)
86
+ copy(dst_buffer, src)
87
+ return dst_buffer
88
+ else:
89
+ copy(dst_buffer, src)
90
+ return None
91
+
92
+ if dst_buffer is None:
93
+ dst_buffer = Image(src.width, src.height)
94
+ return_buffer = True
95
+ else:
96
+ ensure_capacity(dst_buffer, src.width, src.height)
97
+ return_buffer = False
98
+
99
+ sharpen_f32(
100
+ src._buffer._handle,
101
+ dst_buffer._buffer._handle,
102
+ src.width,
103
+ src.height,
104
+ float(strength)
105
+ )
106
+
107
+ return dst_buffer if return_buffer else None
108
+
109
+ @staticmethod
110
+ def sepia(image: Image, intensity: float = 1.0) -> None:
111
+ """
112
+ Applies Sepia tone (in-place).
113
+
114
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#sepia
115
+ """
116
+ if abs(intensity) < 1e-6:
117
+ return
118
+
119
+ sepia_f32(image._buffer._handle, image.width, image.height, float(intensity))
120
+
121
+ @staticmethod
122
+ def invert(image: Image) -> None:
123
+ """
124
+ Inverts colors (Negative effect) in-place.
125
+
126
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#invert
127
+ """
128
+ invert_f32(image._buffer._handle, image.width, image.height)
129
+
130
+ @staticmethod
131
+ def threshold(image: Image, value: float = 0.5) -> None:
132
+ """
133
+ Converts to pure Black & White based on luminance threshold.
134
+ value: 0.0 to 1.0. Pixels brighter than value become white, others black.
135
+
136
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#threshold
137
+ """
138
+ threshold_f32(image._buffer._handle, image.width, image.height, float(value))
139
+
140
+ @staticmethod
141
+ def solarize(image: Image, threshold: float = 0.5) -> None:
142
+ """
143
+ Inverts only pixels brighter than threshold. Creates a psychedelic/retro look.
144
+
145
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#solarize
146
+ """
147
+ solarize_f32(image._buffer._handle, image.width, image.height, float(threshold))
148
+
149
+ @staticmethod
150
+ def sobel(src: Image, dst_buffer: Image | None = None) -> Image | None:
151
+ """
152
+ Detects edges using Sobel operator. Returns a black & white image with edges.
153
+
154
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#sobel
155
+ """
156
+ if dst_buffer is None:
157
+ dst_buffer = Image(src.width, src.height)
158
+ return_buffer = True
159
+ else:
160
+ ensure_capacity(dst_buffer, src.width, src.height)
161
+ return_buffer = False
162
+
163
+ filter_sobel_f32(
164
+ src._buffer._handle,
165
+ dst_buffer._buffer._handle,
166
+ src.width, src.height
167
+ )
168
+ return dst_buffer if return_buffer else None
169
+
170
+ @staticmethod
171
+ def emboss(src: Image, strength: float = 1.0, dst_buffer: Image | None = None) -> Image | None:
172
+ """
173
+ Applies Emboss (Relief) effect.
174
+
175
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/filter/#emboss
176
+ """
177
+ if abs(strength) < 1e-6:
178
+ if dst_buffer is None:
179
+ dst_buffer = Image(src.width, src.height)
180
+ copy(dst_buffer, src)
181
+ return dst_buffer
182
+ else:
183
+ copy(dst_buffer, src)
184
+ return None
185
+
186
+ if dst_buffer is None:
187
+ dst_buffer = Image(src.width, src.height)
188
+ return_buffer = True
189
+ else:
190
+ ensure_capacity(dst_buffer, src.width, src.height)
191
+ return_buffer = False
192
+
193
+ filter_emboss_f32(
194
+ src._buffer._handle,
195
+ dst_buffer._buffer._handle,
196
+ src.width, src.height, float(strength)
197
+ )
198
+ return dst_buffer if return_buffer else None
pyimagecuda/image.py ADDED
@@ -0,0 +1,95 @@
1
+ from .pyimagecuda_internal import create_buffer_f32, free_buffer, create_buffer_u8 #type: ignore
2
+
3
+
4
+ class Buffer:
5
+
6
+ def __init__(self, width: int, height: int, is_u8: bool = False):
7
+ create_func = create_buffer_u8 if is_u8 else create_buffer_f32
8
+ self._handle = create_func(width, height)
9
+ self.capacity_width = width
10
+ self.capacity_height = height
11
+
12
+ def free(self) -> None:
13
+ free_buffer(self._handle)
14
+
15
+
16
+ class ImageBase:
17
+
18
+ def __init__(self, width: int, height: int, is_u8: bool = False):
19
+ self._buffer = Buffer(width, height, is_u8)
20
+ self._width = width
21
+ self._height = height
22
+
23
+ @property
24
+ def width(self) -> int:
25
+ return self._width
26
+
27
+ @width.setter
28
+ def width(self, value: int) -> None:
29
+ value = int(value)
30
+ if value <= 0:
31
+ raise ValueError(f"Width must be positive, got {value}")
32
+
33
+ if value > self._buffer.capacity_width:
34
+ raise ValueError(
35
+ f"Width {value} exceeds buffer capacity "
36
+ f"{self._buffer.capacity_width}"
37
+ )
38
+
39
+ self._width = value
40
+
41
+ @property
42
+ def height(self) -> int:
43
+ return self._height
44
+
45
+ @height.setter
46
+ def height(self, value: int) -> None:
47
+ value = int(value)
48
+ if value <= 0:
49
+ raise ValueError(f"Height must be positive, got {value}")
50
+
51
+ if value > self._buffer.capacity_height:
52
+ raise ValueError(
53
+ f"Height {value} exceeds buffer capacity "
54
+ f"{self._buffer.capacity_height}"
55
+ )
56
+
57
+ self._height = value
58
+
59
+ def free(self) -> None:
60
+ self._buffer.free()
61
+
62
+ def __enter__(self):
63
+ return self
64
+
65
+ def __exit__(self, exc_type, exc_value, traceback):
66
+ self.free()
67
+ return False
68
+
69
+ def get_max_capacity(self) -> tuple[int, int]:
70
+ return (self._buffer.capacity_width, self._buffer.capacity_height)
71
+
72
+ def __repr__(self) -> str:
73
+ return f"{self.__class__.__name__}({self.width}×{self.height})"
74
+
75
+
76
+ class Image(ImageBase):
77
+
78
+ def __init__(self, width: int, height: int):
79
+ """
80
+ Creates a floating-point image with the given width and height.
81
+
82
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/image/#image-float32-precision
83
+ """
84
+ super().__init__(width, height, is_u8=False)
85
+
86
+
87
+ class ImageU8(ImageBase):
88
+
89
+ def __init__(self, width: int, height: int):
90
+ """
91
+ Creates an 8-bit unsigned integer image with the given width and height.
92
+
93
+ Docs & Examples: https://offerrall.github.io/pyimagecuda/image/#imageu8-8-bit-precision
94
+ """
95
+ super().__init__(width, height, is_u8=True)