python again

This commit is contained in:
Chris Tsang
2023-09-16 17:59:52 +01:00
parent 79dd451da1
commit f5cce867f2

View File

@@ -1,28 +1,29 @@
use crate::*;
use pyo3::prelude::*; use pyo3::prelude::*;
use std::path::PathBuf;
use visioncortex::{PathSimplifyMode}; use visioncortex::PathSimplifyMode;
use super::converter::*;
/// Python binding /// Python binding
#[pyfunction] #[pyfunction]
fn convert_image_to_svg_py( image_path: &str, fn convert_image_to_svg_py(
out_path: &str, image_path: &str,
colormode: Option<&str>, // "color" or "binary" out_path: &str,
hierarchical: Option<&str>, // "stacked" or "cutout" colormode: Option<&str>, // "color" or "binary"
mode: Option<&str>, // "polygon", "spline", "none" hierarchical: Option<&str>, // "stacked" or "cutout"
filter_speckle: Option<usize>, // default: 4 mode: Option<&str>, // "polygon", "spline", "none"
color_precision: Option<i32>, // default: 6 filter_speckle: Option<usize>, // default: 4
layer_difference: Option<i32>, // default: 16 color_precision: Option<i32>, // default: 6
corner_threshold: Option<i32>, // default: 60 layer_difference: Option<i32>, // default: 16
length_threshold: Option<f64>, // in [3.5, 10] default: 4.0 corner_threshold: Option<i32>, // default: 60
max_iterations: Option<usize>, // default: 10 length_threshold: Option<f64>, // in [3.5, 10] default: 4.0
splice_threshold: Option<i32>, // default: 45 max_iterations: Option<usize>, // default: 10
path_precision: Option<u32> // default: 8 splice_threshold: Option<i32>, // default: 45
) -> PyResult<()> { path_precision: Option<u32>, // default: 8
) -> PyResult<()> {
let input_path = PathBuf::from(image_path); let input_path = PathBuf::from(image_path);
let output_path = PathBuf::from(out_path); let output_path = PathBuf::from(out_path);
// TODO: enforce color mode with an enum so that we only // TODO: enforce color mode with an enum so that we only
// accept the strings 'color' or 'binary' // accept the strings 'color' or 'binary'
let color_mode = match colormode.unwrap_or("color") { let color_mode = match colormode.unwrap_or("color") {
"color" => ColorMode::Color, "color" => ColorMode::Color,
@@ -35,7 +36,7 @@ fn convert_image_to_svg_py( image_path: &str,
"cutout" => Hierarchical::Cutout, "cutout" => Hierarchical::Cutout,
_ => Hierarchical::Stacked, _ => Hierarchical::Stacked,
}; };
let mode = match mode.unwrap_or("spline") { let mode = match mode.unwrap_or("spline") {
"spline" => PathSimplifyMode::Spline, "spline" => PathSimplifyMode::Spline,
"polygon" => PathSimplifyMode::Polygon, "polygon" => PathSimplifyMode::Polygon,
@@ -43,13 +44,13 @@ fn convert_image_to_svg_py( image_path: &str,
_ => PathSimplifyMode::Spline, _ => PathSimplifyMode::Spline,
}; };
let filter_speckle = filter_speckle.unwrap_or(4); let filter_speckle = filter_speckle.unwrap_or(4);
let color_precision = color_precision.unwrap_or(6); let color_precision = color_precision.unwrap_or(6);
let layer_difference = layer_difference.unwrap_or(16); let layer_difference = layer_difference.unwrap_or(16);
let corner_threshold = corner_threshold.unwrap_or(60); let corner_threshold = corner_threshold.unwrap_or(60);
let length_threshold = length_threshold.unwrap_or(4.0); let length_threshold = length_threshold.unwrap_or(4.0);
let splice_threshold = splice_threshold.unwrap_or(45); let splice_threshold = splice_threshold.unwrap_or(45);
let max_iterations = max_iterations.unwrap_or(10); let max_iterations = max_iterations.unwrap_or(10);
let config = Config { let config = Config {
input_path, input_path,
@@ -65,10 +66,9 @@ fn convert_image_to_svg_py( image_path: &str,
max_iterations, max_iterations,
splice_threshold, splice_threshold,
path_precision, path_precision,
..Default::default() ..Default::default()
}; };
convert_image_to_svg(config).unwrap(); convert_image_to_svg(config).unwrap();
Ok(()) Ok(())
} }
@@ -78,4 +78,4 @@ fn convert_image_to_svg_py( image_path: &str,
fn vtracer(_py: Python, m: &PyModule) -> PyResult<()> { fn vtracer(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(convert_image_to_svg_py, m)?)?; m.add_function(wrap_pyfunction!(convert_image_to_svg_py, m)?)?;
Ok(()) Ok(())
} }