Move code around

This commit is contained in:
Chris Tsang
2023-09-08 12:59:28 +01:00
parent c3090ac50b
commit 02c844dc7a
10 changed files with 98 additions and 105 deletions

View File

@@ -7,9 +7,6 @@ name: CI
on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:

View File

@@ -1,7 +1,7 @@
[package]
name = "vtracer"
version = "0.6.3"
authors = ["Chris Tsang <tyt2y7@gmail.com>"]
authors = ["Chris Tsang <chris.2y3@outlook.com>"]
edition = "2021"
description = "A cmd app to convert images into vector graphics."
license = "MIT OR Apache-2.0"
@@ -15,13 +15,7 @@ clap = "2.33.3"
image = "0.23.10"
visioncortex = { version = "0.8.0" }
fastrand = "1.8"
pyo3 = "0.19.0"
pyo3 = { version = "0.19.0", optional = true }
[lib]
name = "vtracer"
crate-type = ["cdylib"]
path = "src/lib.rs"
[[bin]]
name = "vtracer"
path = "src/main.rs"
[features]
python-binding = ["pyo3"]

View File

@@ -2,8 +2,8 @@
name = "vtracer"
version = "0.6.3"
description = "Python bindings for the Rust Vtracer raster-to-vector library"
authors = [ { name = "Chris Tsang", email = "tyt2y7@gmail.com" } ]
readme = "vtracer/README_PY.md"
authors = [ { name = "Chris Tsang", email = "chris.2y3@outlook.com" } ]
readme = "vtracer/README.md"
requires-python = ">=3.7"
license = "MIT"
classifiers = [
@@ -25,4 +25,4 @@ build-backend = "maturin"
[tool.maturin]
features = ["pyo3/extension-module"]
compatibility = "linux"
sdist-include = ["../LICENSE", "../README_PY.md"]
sdist-include = ["../LICENSE", "../README.md"]

View File

@@ -1,9 +1,8 @@
use std::path::PathBuf;
use std::{fs::File, io::Write};
use pyo3::prelude::*;
use fastrand::Rng;
use visioncortex::{Color, ColorImage, ColorName, PathSimplifyMode};
use visioncortex::{Color, ColorImage, ColorName};
use visioncortex::color_clusters::{Runner, RunnerConfig, KeyingAction, HIERARCHICAL_MAX};
use super::config::{Config, ColorMode, Hierarchical, ConverterConfig};
use super::svg::SvgFile;
@@ -225,84 +224,5 @@ fn write_svg(svg: SvgFile, output_path: PathBuf) -> Result<(), String> {
write!(&mut out_file, "{}", svg).expect("failed to write file.");
Ok(())
}
// ==================
// = PYTHON BINDING =
// ==================
#[pyfunction]
fn convert_image_to_svg_py( image_path: &str,
out_path: &str,
colormode: Option<&str>, // "color" or "binary"
hierarchical: Option<&str>, // "stacked" or "cutout"
mode: Option<&str>, // "polygon", "spline", "none"
filter_speckle: Option<usize>, // default: 4
color_precision: Option<i32>, // default: 6
layer_difference: Option<i32>, // default: 16
corner_threshold: Option<i32>, // default: 60
length_threshold: Option<f64>, // in [3.5, 10] default: 4.0
max_iterations: Option<usize>, // default: 10
splice_threshold: Option<i32>, // default: 45
path_precision: Option<u32> // default: 8
) -> PyResult<()> {
let input_path = PathBuf::from(image_path);
let output_path = PathBuf::from(out_path);
// TODO: enforce color mode with an enum so that we only
// accept the strings 'color' or 'binary'
let color_mode = match colormode.unwrap_or("color") {
"color" => ColorMode::Color,
"binary" => ColorMode::Binary,
_ => ColorMode::Color,
};
let hierarchical = match hierarchical.unwrap_or("stacked") {
"stacked" => Hierarchical::Stacked,
"cutout" => Hierarchical::Cutout,
_ => Hierarchical::Stacked,
};
let mode = match mode.unwrap_or("spline") {
"spline" => PathSimplifyMode::Spline,
"polygon" => PathSimplifyMode::Polygon,
"none" => PathSimplifyMode::None,
_ => PathSimplifyMode::Spline,
};
let filter_speckle = filter_speckle.unwrap_or(4);
let color_precision = color_precision.unwrap_or(6);
let layer_difference = layer_difference.unwrap_or(16);
let corner_threshold = corner_threshold.unwrap_or(60);
let length_threshold = length_threshold.unwrap_or(4.0);
let splice_threshold = splice_threshold.unwrap_or(45);
let max_iterations = max_iterations.unwrap_or(10);
let config = Config {
input_path,
output_path,
color_mode,
hierarchical,
filter_speckle,
color_precision,
layer_difference,
mode,
corner_threshold,
length_threshold,
max_iterations,
splice_threshold,
path_precision,
..Default::default()
};
convert_image_to_svg(config).unwrap();
Ok(())
}
/// A Python module implemented in Rust.
#[pymodule]
fn vtracer(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(convert_image_to_svg_py, m)?)?;
Ok(())
}

View File

@@ -11,7 +11,10 @@
mod config;
mod converter;
mod svg;
mod python;
pub use config::*;
pub use converter::*;
pub use svg::*;
pub use svg::*;
#[cfg(feature = "python-binding")]
pub use python::*;

View File

@@ -1,10 +1,8 @@
mod config;
mod converter;
mod svg;
use vtracer::{Config, convert_image_to_svg};
fn main() {
let config = config::Config::from_args();
let result = converter::convert_image_to_svg(config);
let config = Config::from_args();
let result = convert_image_to_svg(config);
match result {
Ok(()) => {
println!("Conversion successful.");

81
cmdapp/src/python.rs Normal file
View File

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

View File

@@ -1,7 +1,7 @@
[package]
name = "vtracer-webapp"
version = "0.4.0"
authors = ["Chris Tsang <tyt2y7@gmail.com>"]
authors = ["Chris Tsang <chris.2y3@outlook.com>"]
edition = "2021"
description = "A web app to convert images into vector graphics."
license = "MIT OR Apache-2.0"

View File

@@ -2,7 +2,7 @@
"name": "vtracer-app",
"version": "0.1.0",
"description": "VTracer Webapp",
"author": "Chris Tsang <tyt2y7@gmail.com>",
"author": "Chris Tsang <chris.2y3@outlook.com>",
"license": "proprietary",
"private": true,
"main": "index.js",