reference-image-pixel-regions 0.1.0__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.
- reference_image_pixel_regions/__init__.py +45 -0
- reference_image_pixel_regions-0.1.0.dist-info/METADATA +41 -0
- reference_image_pixel_regions-0.1.0.dist-info/RECORD +6 -0
- reference_image_pixel_regions-0.1.0.dist-info/WHEEL +5 -0
- reference_image_pixel_regions-0.1.0.dist-info/licenses/LICENSE +21 -0
- reference_image_pixel_regions-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Convert normalized reference-image annotations into integer bounds."""
|
|
2
|
+
|
|
3
|
+
from decimal import Decimal
|
|
4
|
+
from fractions import Fraction
|
|
5
|
+
from typing import NamedTuple, Union
|
|
6
|
+
|
|
7
|
+
Coordinate = Union[int, float, str, Decimal, Fraction]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PixelRegion(NamedTuple):
|
|
11
|
+
left: int
|
|
12
|
+
top: int
|
|
13
|
+
right: int
|
|
14
|
+
bottom: int
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _coordinate(value: Coordinate) -> Fraction:
|
|
18
|
+
if isinstance(value, bool):
|
|
19
|
+
raise ValueError("coordinates must be numbers between zero and one")
|
|
20
|
+
try:
|
|
21
|
+
number = value if isinstance(value, Fraction) else Fraction(str(value))
|
|
22
|
+
except (ValueError, ZeroDivisionError) as error:
|
|
23
|
+
raise ValueError("coordinates must be finite numbers") from error
|
|
24
|
+
if not 0 <= number <= 1:
|
|
25
|
+
raise ValueError("coordinates must be between zero and one")
|
|
26
|
+
return number
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def enclosing_pixels(
|
|
30
|
+
width: int, height: int, left: Coordinate, top: Coordinate,
|
|
31
|
+
right: Coordinate, bottom: Coordinate,
|
|
32
|
+
) -> PixelRegion:
|
|
33
|
+
"""Enclose a top-left-origin selection in half-open pixel bounds."""
|
|
34
|
+
if any(type(size) is not int or size <= 0 for size in (width, height)):
|
|
35
|
+
raise ValueError("image dimensions must be positive integers")
|
|
36
|
+
x0, y0, x1, y1 = map(_coordinate, (left, top, right, bottom))
|
|
37
|
+
if x0 >= x1 or y0 >= y1:
|
|
38
|
+
raise ValueError("the selection must have positive width and height")
|
|
39
|
+
|
|
40
|
+
return PixelRegion(
|
|
41
|
+
(x0 * width).__floor__(),
|
|
42
|
+
(y0 * height).__floor__(),
|
|
43
|
+
(x1 * width).__ceil__(),
|
|
44
|
+
(y1 * height).__ceil__(),
|
|
45
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reference-image-pixel-regions
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert normalized reference-image selections to enclosing pixel regions.
|
|
5
|
+
Author: js-upup
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Reference image workspace, https://aih3image.com/minimax-h3-image-to-image
|
|
8
|
+
Keywords: reference-image,annotations,pixel-coordinates
|
|
9
|
+
Requires-Python: >=3.9
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# Reference Image Pixel Regions
|
|
15
|
+
|
|
16
|
+
Convert normalized annotations into the smallest integer pixel rectangle that
|
|
17
|
+
fully contains the selection. This is useful when an annotation arrives as
|
|
18
|
+
fractions of the image width and height and a cropper expects pixel coordinates.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install reference-image-pixel-regions
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from reference_image_pixel_regions import enclosing_pixels
|
|
28
|
+
|
|
29
|
+
region = enclosing_pixels(1200, 800, "0.25", "0.10", "0.75", "0.50")
|
|
30
|
+
# PixelRegion(left=300, top=80, right=900, bottom=400)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The origin is the top left. Right and bottom are exclusive. Supply coordinates
|
|
34
|
+
in the range 0 to 1. Decimal strings preserve the intended boundary. The
|
|
35
|
+
library works with dimensions and coordinates and leaves image I/O and editing
|
|
36
|
+
to the caller. It requires Python 3.9 or newer and uses the standard library.
|
|
37
|
+
|
|
38
|
+
For the related online editing workflow, see the
|
|
39
|
+
[AI H3 Image reference-image workspace](https://aih3image.com/minimax-h3-image-to-image).
|
|
40
|
+
|
|
41
|
+
MIT license.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
reference_image_pixel_regions/__init__.py,sha256=qLQcW2SrPYI9KJzcWXEzzYRqjHbQxf8bLMRm2a3rg3A,1518
|
|
2
|
+
reference_image_pixel_regions-0.1.0.dist-info/licenses/LICENSE,sha256=Vm-Lx6SkWO7gJRvMYyHjPcUoYGeEntaSqYo5UEZ2ags,1064
|
|
3
|
+
reference_image_pixel_regions-0.1.0.dist-info/METADATA,sha256=-4VGEsryxuhQ6jmGq0EGDClYHfYzA4TfiPNUMyKxHQ4,1443
|
|
4
|
+
reference_image_pixel_regions-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
reference_image_pixel_regions-0.1.0.dist-info/top_level.txt,sha256=HPLCuHI9txV2hhrmXu58q9GMqb2pvF4iZAK-dbwMeJY,30
|
|
6
|
+
reference_image_pixel_regions-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 js-upup
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
reference_image_pixel_regions
|