Skip to content

Commit

Permalink
fix: Remove dependency on Numpy and Pillow
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Apr 7, 2023
1 parent c3b7a16 commit ee54831
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 113 deletions.
78 changes: 0 additions & 78 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ name = "resvg_py"
crate-type = ["cdylib"]

[dependencies]
numpy = "0.18.0"
pyo3 = "0.18.1"
resvg = "0.30.0"
tiny-skia = "0.8.3"
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ Overall, `resvg-py` provides a simple and powerful way to work with SVG images i
from pathlib import Path

import resvg_py
from PIL import Image

options = resvg_py.SVGOptions()
r = resvg_py.Resvg(options)

with Path("resources/examples/svg/octocat.svg").open("r") as f:
rendered = r.render(f.read(), 400, 400)
array = rendered.as_array()
im = Image.fromarray(array)
im.save("image.png")

with open("image.png", "wb") as f:
f.write(rendered.as_png())
```

# Options
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"numpy",
"Pillow",
]
dependencies = []

[project.optional-dependencies]
dev = ["pytest", "pytest-snapshot"]
Expand Down
26 changes: 0 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use numpy::ndarray::Array3;
use numpy::{IntoPyArray, PyArray3};
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use resvg::tiny_skia::Pixmap;
Expand Down Expand Up @@ -128,30 +126,6 @@ impl RenderedImage {
self.pixmap.height()
}

/// Returns the rendered image as a numpy array.
///
/// The array has shape (height, width, 4) and contains RGBA data.
pub fn as_array<'py>(&self, py: Python<'py>) -> PyResult<&'py PyArray3<u8>> {
let pixels = self.pixmap.pixels();

Ok(Array3::from_shape_fn(
((self.height()) as usize, (self.width()) as usize, 4),
|(y, x, c)| {
let index = y * self.width() as usize + x;
let pixel = &pixels[index];

match c {
0 => pixel.red(),
1 => pixel.green(),
2 => pixel.blue(),
3 => pixel.alpha(),
_ => unreachable!(),
}
},
)
.into_pyarray(py))
}

/// Returns the rendered image as bytes in PNG format.
fn as_png(&self, py: Python) -> PyResult<PyObject> {
self.pixmap
Expand Down

0 comments on commit ee54831

Please sign in to comment.