svg-ultralight 0.35.1__tar.gz → 0.36.0__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.

Potentially problematic release.


This version of svg-ultralight might be problematic. Click here for more details.

Files changed (50) hide show
  1. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/.pre-commit-config.yaml +5 -5
  2. {svg_ultralight-0.35.1/src/svg_ultralight.egg-info → svg_ultralight-0.36.0}/PKG-INFO +3 -1
  3. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/pyproject.toml +3 -2
  4. svg_ultralight-0.36.0/src/svg_ultralight/image_ops.py +134 -0
  5. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0/src/svg_ultralight.egg-info}/PKG-INFO +3 -1
  6. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight.egg-info/SOURCES.txt +1 -0
  7. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight.egg-info/requires.txt +3 -0
  8. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/.gitignore +0 -0
  9. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/README.md +0 -0
  10. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/setup.cfg +0 -0
  11. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/__init__.py +0 -0
  12. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/animate.py +0 -0
  13. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/__init__.py +0 -0
  14. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/bound_helpers.py +0 -0
  15. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/supports_bounds.py +0 -0
  16. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/type_bound_collection.py +0 -0
  17. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/type_bound_element.py +0 -0
  18. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/type_bounding_box.py +0 -0
  19. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/bounding_boxes/type_padded_text.py +0 -0
  20. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/constructors/__init__.py +0 -0
  21. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/constructors/new_element.py +0 -0
  22. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/inkscape.py +0 -0
  23. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/layout.py +0 -0
  24. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/main.py +0 -0
  25. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/metadata.py +0 -0
  26. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/nsmap.py +0 -0
  27. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/py.typed +0 -0
  28. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/query.py +0 -0
  29. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/root_elements.py +0 -0
  30. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/string_conversion.py +0 -0
  31. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/strings/__init__.py +0 -0
  32. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/strings/svg_strings.py +0 -0
  33. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/transformations.py +0 -0
  34. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight/unit_conversion.py +0 -0
  35. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight.egg-info/dependency_links.txt +0 -0
  36. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/src/svg_ultralight.egg-info/top_level.txt +0 -0
  37. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/__init__.py +0 -0
  38. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/conftest.py +0 -0
  39. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/resources/arrow.svg +0 -0
  40. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_bounding.py +0 -0
  41. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_inkscape.py +0 -0
  42. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_layout.py +0 -0
  43. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_matrices.py +0 -0
  44. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_metadata.py +0 -0
  45. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_new_element.py +0 -0
  46. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_queries.py +0 -0
  47. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_root_elements.py +0 -0
  48. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_string_conversion.py +0 -0
  49. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tests/test_svg_ultralight.py +0 -0
  50. {svg_ultralight-0.35.1 → svg_ultralight-0.36.0}/tox.ini +0 -0
@@ -42,12 +42,12 @@ repos:
42
42
  # files: .pre-commit-config.yaml
43
43
 
44
44
  - repo: https://github.com/pre-commit/mirrors-mypy
45
- rev: v1.13.0
45
+ rev: v1.14.0
46
46
  hooks:
47
47
  - id: mypy
48
48
  name: mypy
49
49
  language: python
50
- language_version: python 3.12
50
+ language_version: python 3.11
51
51
  types: [python]
52
52
  require_serial: true
53
53
  verbose: true
@@ -58,7 +58,7 @@ repos:
58
58
  # files: ^(src/|tests/)
59
59
 
60
60
  - repo: https://github.com/asottile/pyupgrade
61
- rev: v3.19.0
61
+ rev: v3.19.1
62
62
  hooks:
63
63
  - args:
64
64
  - --py39-plus
@@ -92,7 +92,7 @@ repos:
92
92
  # S301 don't use pickle
93
93
  # B028 wants explicit stacklevel on warn
94
94
  # BLE001 Use of `except Exception:` detected
95
- rev: 'v0.8.3'
95
+ rev: 'v0.8.4'
96
96
  hooks:
97
97
  - id: ruff
98
98
  name: "ruff-lint"
@@ -110,6 +110,6 @@ repos:
110
110
 
111
111
  # reads pyproject.toml for additional config
112
112
  - repo: https://github.com/RobertCraigie/pyright-python
113
- rev: v1.1.390
113
+ rev: v1.1.391
114
114
  hooks:
115
115
  - id: pyright
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: svg-ultralight
3
- Version: 0.35.1
3
+ Version: 0.36.0
4
4
  Summary: a sensible way to create svg files with Python
5
5
  Author-email: Shay Hill <shay_public@hotmail.com>
6
6
  License: MIT
@@ -15,6 +15,8 @@ Requires-Dist: pytest; extra == "dev"
15
15
  Requires-Dist: commitizen; extra == "dev"
16
16
  Requires-Dist: pre-commit; extra == "dev"
17
17
  Requires-Dist: tox; extra == "dev"
18
+ Provides-Extra: images
19
+ Requires-Dist: pillow; extra == "images"
18
20
 
19
21
  # svg_ultralight
20
22
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "svg-ultralight"
3
- version = "0.35.1"
3
+ version = "0.36.0"
4
4
  description = "a sensible way to create svg files with Python"
5
5
  authors = [{ name = "Shay Hill", email = "shay_public@hotmail.com" }]
6
6
  license = { text = "MIT" }
@@ -10,6 +10,7 @@ dependencies = ["lxml", "pillow", "paragraphs", "types-lxml"]
10
10
 
11
11
  [project.optional-dependencies]
12
12
  dev = ["pytest", "commitizen", "pre-commit", "tox"]
13
+ images = ["pillow"]
13
14
 
14
15
  [build-system]
15
16
  requires = ["setuptools", "setuptools-scm"]
@@ -41,7 +42,7 @@ convention = "pep257"
41
42
 
42
43
  [tool.commitizen]
43
44
  name = "cz_conventional_commits"
44
- version = "0.35.1"
45
+ version = "0.36.0"
45
46
  tag_format = "$version"
46
47
  version_files = ["pyproject.toml:^version"]
47
48
  annotated_tag = true
@@ -0,0 +1,134 @@
1
+ """Crop an image before converting to binary and including in the svg file.
2
+
3
+ This optional module requires the Pillow library. Create an svg image element with a
4
+ rasterized image positioned inside a bounding box.
5
+
6
+ :author: Shay Hill
7
+ :created: 2024-11-20
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from typing import TYPE_CHECKING
13
+
14
+ from paragraphs import par
15
+
16
+ try:
17
+ from PIL import Image
18
+
19
+ if TYPE_CHECKING:
20
+ from PIL.Image import Image as ImageType
21
+ except ImportError as err:
22
+ msg = par(
23
+ """PIL is not installed. Install it using 'pip install Pillow' to use
24
+ svg_ultralight.image_ops module."""
25
+ )
26
+ raise ImportError(msg) from err
27
+
28
+ import base64
29
+ import io
30
+
31
+ from lxml import etree
32
+
33
+ from svg_ultralight import NSMAP
34
+ from svg_ultralight.bounding_boxes.bound_helpers import bbox_dict
35
+ from svg_ultralight.constructors import new_element
36
+
37
+ if TYPE_CHECKING:
38
+ from pathlib import Path
39
+
40
+ from lxml.etree import (
41
+ _Element as EtreeElement, # pyright: ignore [reportPrivateUsage]
42
+ )
43
+
44
+ from svg_ultralight.bounding_boxes.type_bounding_box import BoundingBox
45
+
46
+
47
+ def _symmetric_crop(
48
+ image: ImageType, center: tuple[float, float] | None = None
49
+ ) -> ImageType:
50
+ """Crop an image symmetrically around a center point.
51
+
52
+ :param image: PIL.Image instance
53
+ :param center: optional center point for cropping. Proportions of image with and
54
+ image height, so the default value, (0.5, 0.5), is the true center of the
55
+ image. (0.4, 0.5) would crop 20% off the right side of the image.
56
+ :return: PIL.Image instance
57
+ """
58
+ if center is None:
59
+ return image
60
+
61
+ if not all(0 < x < 1 for x in center):
62
+ msg = "Center must be between (0, 0) and (1, 1)"
63
+ raise ValueError(msg)
64
+
65
+ xd, yd = (min(x, 1 - x) for x in center)
66
+ left, right = sorted(x * image.width for x in (center[0] - xd, center[0] + xd))
67
+ top, bottom = sorted(x * image.height for x in (center[1] - yd, center[1] + yd))
68
+
69
+ return image.crop((left, top, right, bottom))
70
+
71
+
72
+ def _crop_image_to_bbox_ratio(
73
+ image: ImageType, bbox: BoundingBox, center: tuple[float, float] | None = None
74
+ ) -> ImageType:
75
+ """Crop an image to the ratio of a bounding box.
76
+
77
+ :param image: PIL.Image instance
78
+ :param bbox: BoundingBox instance
79
+ :param center: optional center point for cropping. Proportions of image with and
80
+ image height, so the default value, (0.5, 0.5), is the true center of the
81
+ image. (0.4, 0.5) would crop 20% off the right side of the image.
82
+ :return: PIL.Image instance
83
+
84
+ This crops the image to the specified ratio. It's not a resize, so it will cut
85
+ off the top and bottom or the sides of the image to fit the ratio.
86
+ """
87
+ image = _symmetric_crop(image, center)
88
+ width, height = image.size
89
+
90
+ ratio = bbox.width / bbox.height
91
+ if width / height > ratio:
92
+ new_width = height * ratio
93
+ left = (width - new_width) / 2
94
+ right = width - left
95
+ return image.crop((left, 0, right, height))
96
+ new_height = width / ratio
97
+ top = (height - new_height) / 2
98
+ bottom = height - top
99
+ return image.crop((0, top, width, bottom))
100
+
101
+
102
+ def _get_svg_embedded_image_str(image: ImageType) -> str:
103
+ """Return the string you'll need to embed an image in an svg.
104
+
105
+ :param image: PIL.Image instance
106
+ :return: argument for xlink:href
107
+ """
108
+ in_mem_file = io.BytesIO()
109
+ image.save(in_mem_file, format="PNG")
110
+ _ = in_mem_file.seek(0)
111
+ img_bytes = in_mem_file.read()
112
+ base64_encoded_result_bytes = base64.b64encode(img_bytes)
113
+ base64_encoded_result_str = base64_encoded_result_bytes.decode("ascii")
114
+ return "data:image/png;base64," + base64_encoded_result_str
115
+
116
+
117
+ def new_image_elem_in_bbox(
118
+ filename: Path | str, bbox: BoundingBox, center: tuple[float, float] | None
119
+ ) -> EtreeElement:
120
+ """Create a new svg image element inside a bounding box.
121
+
122
+ :param filename: filename of source image
123
+ :param bbox: bounding box for the image
124
+ :param center: center point for cropping. Proportions of image width and image
125
+ height, so the default value, (0.5, 0.5), is the true center of the image.
126
+ (0.4, 0.5) would crop 20% off the right side of the image.
127
+ :return: an etree image element with the cropped image embedded
128
+ """
129
+ image = _crop_image_to_bbox_ratio(Image.open(filename), bbox, center)
130
+ svg_image = new_element("image", **bbox_dict(bbox))
131
+ svg_image.set(
132
+ etree.QName(NSMAP["xlink"], "href"), _get_svg_embedded_image_str(image)
133
+ )
134
+ return svg_image
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: svg-ultralight
3
- Version: 0.35.1
3
+ Version: 0.36.0
4
4
  Summary: a sensible way to create svg files with Python
5
5
  Author-email: Shay Hill <shay_public@hotmail.com>
6
6
  License: MIT
@@ -15,6 +15,8 @@ Requires-Dist: pytest; extra == "dev"
15
15
  Requires-Dist: commitizen; extra == "dev"
16
16
  Requires-Dist: pre-commit; extra == "dev"
17
17
  Requires-Dist: tox; extra == "dev"
18
+ Provides-Extra: images
19
+ Requires-Dist: pillow; extra == "images"
18
20
 
19
21
  # svg_ultralight
20
22
 
@@ -5,6 +5,7 @@ pyproject.toml
5
5
  tox.ini
6
6
  src/svg_ultralight/__init__.py
7
7
  src/svg_ultralight/animate.py
8
+ src/svg_ultralight/image_ops.py
8
9
  src/svg_ultralight/inkscape.py
9
10
  src/svg_ultralight/layout.py
10
11
  src/svg_ultralight/main.py
@@ -8,3 +8,6 @@ pytest
8
8
  commitizen
9
9
  pre-commit
10
10
  tox
11
+
12
+ [images]
13
+ pillow
File without changes