Skip to content

Commit 43a0de7

Browse files
committed
add image method to python lib
1 parent d4287e1 commit 43a0de7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from processing import *
2+
3+
def setup():
4+
size(800, 600)
5+
6+
def draw():
7+
background(220)
8+
image("images/logo.png")
9+
10+
11+
# TODO: this should happen implicitly on module load somehow
12+
run()

crates/processing_pyo3/src/graphics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ impl Graphics {
122122
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
123123
}
124124

125+
pub fn image(&self, file: &str) -> PyResult<()> {
126+
let image = image_load(file).unwrap();
127+
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image))
128+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
129+
}
130+
125131
pub fn push_matrix(&self) -> PyResult<()> {
126132
graphics_record_command(self.entity, DrawCommand::PushMatrix)
127133
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))

crates/processing_pyo3/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2626
m.add_function(wrap_pyfunction!(no_stroke, m)?)?;
2727
m.add_function(wrap_pyfunction!(stroke_weight, m)?)?;
2828
m.add_function(wrap_pyfunction!(rect, m)?)?;
29+
m.add_function(wrap_pyfunction!(image, m)?)?;
2930
Ok(())
3031
}
3132

@@ -122,3 +123,9 @@ fn rect(
122123
) -> PyResult<()> {
123124
get_graphics(module)?.rect(x, y, w, h, tl, tr, br, bl)
124125
}
126+
127+
#[pyfunction]
128+
#[pyo3(pass_module, signature = (image_file))]
129+
fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<()> {
130+
get_graphics(module)?.image(image_file)
131+
}

0 commit comments

Comments
 (0)