Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option for bytes #25

Merged
merged 9 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
baseplate-admin committed May 8, 2024
commit ce5fb9ece10ef0de0da0066c9f4ebb1b56c0cc86
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ svg_string = """
</svg>
"""

print(resvg_py.svg_to_base64(svg_string=svg_string))
print(resvg_py.svg_to_bytes(svg_string=svg_string))

```

Expand Down
24 changes: 22 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,27 @@ We can convert it to `PNG` by:
</svg>
"""

print(resvg_py.svg_to_base64(svg_string=svg_string))
png_bytes: list[bytes] = resvg_py.svg_to_bytes(svg_string=svg_string) # a large list of bytes



In order to convert image to base64:

.. code-block:: python

import resvg_py
import base64

svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
  <rect width="200" height="100" x="10" y="10" rx="20" ry="20" fill="blue" />
</svg>
"""

# a large list of bytes
png_bytes: list[bytes] = resvg_py.svg_to_bytes(svg_string=svg_string)
base64_utf8_str = base64.b64encode(bytes(png_bytes)).decode("utf-8")
print(f"data:image/png;base64,{base64_utf8_str}")


This should return the following **PNG** image (check using inspect element):
Expand All @@ -51,7 +71,7 @@ We can also do something like this :

svg = ... # path to svg file

print(resvg_py.svg_to_base64(svg_path=svg))
print(resvg_py.svg_to_bytes(svg_path=svg))



Expand Down
9 changes: 4 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import resvg_py
import orjson

svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
Expand All @@ -10,10 +9,10 @@
import json
import base64

x = resvg_py.svg_to_bytes(svg_string=svg_string)
print(type(x))
print(type(bytes(x)))
base64_utf8_str = base64.b64encode(bytes(x)).decode("utf-8")

base64_utf8_str = base64.b64encode(
bytes(resvg_py.svg_to_base64(svg_string=svg_string))
).decode("utf-8")
dataurl = f"data:image/png;base64,{base64_utf8_str}"

print(dataurl)
9 changes: 7 additions & 2 deletions tests/test_complex_camera.py

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions tests/test_multiple_layer_svg.py

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions tests/test_normal_shape.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import resvg_py
import base64

svg_string = """
<svg width="300" height="130" xmlns="http://www.w3.org/2000/svg">
Expand All @@ -8,8 +9,10 @@


def test_rectangle():
base64 = resvg_py.svg_to_base64(svg_string=svg_string)
_base64 = base64.b64encode(
bytes(resvg_py.svg_to_bytes(svg_string=svg_string))
).decode("utf-8")
assert (
base64
_base64
== "iVBORw0KGgoAAAANSUhEUgAAASwAAACCCAYAAAD8HPVfAAAIBElEQVR4Ae3gAZAkSZIkSRKLqpm7R0REZmZmVlVVVVV3d3d3d/fMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMdHd3d3dXV1VVVVVmZkZGRIS7m5kKz0xmV3d1d3dPz8zMzMxMorjqqquu+t8BcdVVV131vwPiqquuuup/B8RVV1111f8OiKuuuuqq/x0QV1111VX/OyCuuuqqq/53QFx11VVX/e+AuOqqq6763wFx1VVXXfW/A+I/nV8aeCvgpYEHAy/NVVf9z/HXwK3AXwM/A/prrvqfCvGfwg8GPgp4a+DBXHXV/x63Aj8NfA3oVq76nwTxH8rHga8C3purrvrf77uBj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qr/ndAXHXVVVf974C46qqrrvrfAXHVVVdd9b8D4qqrrrrqfwfEVVddddX/Doirrrrqqv8dEFddddVV/zsgrrrqqqv+d0BcddVVV/3vgLjqqquu+t8BcdVVV131vwP/CCSk/oOTVh6qAAAAAElFTkSuQmCC"
)
13 changes: 3 additions & 10 deletions tests/test_path.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import resvg_py
from pathlib import Path
import os
import base64

BASE_DIR = Path(__file__).resolve().parent

Expand All @@ -9,19 +10,11 @@

def test_path():
path = os.path.join(BASE_DIR, "acid.svg")
print(path)

base = resvg_py.svg_to_base64(svg_path=path)
base = base64.b64encode(bytes(resvg_py.svg_to_bytes(svg_path=path))).decode("utf-8")
assert base == svg_output


def test_gzip_path():
path = os.path.join(BASE_DIR, "acid.svg.gz")
print(path)

base = resvg_py.svg_to_base64(svg_path=path)
base = base64.b64encode(bytes(resvg_py.svg_to_bytes(svg_path=path))).decode("utf-8")
assert base == svg_output


if __name__ == "__main__":
test_path()