#png #image #algorithm #local-file #convert #version #write-file

bin+lib pconvert-rust

Rust 版本的 P(NG)Convert,一个简单的 PNG 转换工具

17 个版本

0.5.1 2023年2月22日
0.5.0 2023年2月13日
0.4.11 2021年9月1日
0.4.10 2021年2月9日
0.2.8 2021年1月3日

#164 in WebAssembly

39 每月下载量

Apache-2.0

6.5MB
3K SLoC

P(NG)Convert Rust

来自 Hive Solutions 的著名 P(NG)Convert 的 Rust 版本。

这个 Rust 包可以作为一个 命令行应用程序,作为一个 crate 在另一个 Rust 项目中,作为一个 Web Assembly 模块(能够在针对网页浏览器或 NodeJS 的 JavaScript 中使用)或作为一个 Python 包

命令行应用程序

编译与执行

使用以下命令构建和运行

cargo run

或者,首先使用以下命令进行编译

cargo build

然后使用以下命令运行二进制文件

./target/debug/pconvert-rust

此外,为了更好的代码优化,使用 --release 标志进行编译

cargo build --release

然后使用以下命令运行发布版二进制文件

./target/release/pconvert-rust

用法

$ pconvert-rust
Usage: pconvert-rust <command> [args...]
where command can be one of the following: compose, convert, benchmark, version
$ pconvert-rust compose <dir>
$ pconvert-rust convert <file_in> <file_out>
$ pconvert-rust benchmark <dir> [--parallel]
$ pconvert-rust version

示例

// blends the provided image as a new image to be used
// under the current instance
let top = pconvert_rust::utils::read_png_from_file("top.png".to_string(), false).unwrap();
let mut bottom = pconvert_rust::utils::read_png_from_file("bottom.png".to_string(), false).unwrap();

// gathers the mask top blending algorithm function and
// uses it to blend both images
let blending_fn = pconvert_rust::blending::get_blending_algorithm(
    &pconvert_rust::blending::BlendAlgorithm::DestinationOver,
);
pconvert_rust::blending::blend_images(&mut bottom, &top, &blending_fn, &None);

// "outputs" the blended image contents to the `out.png` file
pconvert_rust::utils::write_png_to_file_d("out.png".to_string(), &bottom).unwrap();

WebAssembly (WASM) 模块

编译与执行

按照以下指南安装 wasm-pack此指南

要构建,使用 wasm-extension 功能

wasm-pack build -- --features wasm-extension

要运行演示,按照以下指南:

用法

查看以下 演示站点 了解如何使用 PConvert WASM 模块。

暴露的 JavaScript API

// blends two File objects and returns a File object
blendImages(bot, top, targetFileName, algorithm, isInline, options)

// blends two ImageData objects and returns an ImageData object
blendImagesData(bot, top, algorithm, isInline, options)

// blends multiple File objects and returns a File object
blendMultiple(imageFiles, targetFileName, algorithm, algorithms, isInline, options)

// blends multiple ImageData objects  and returns an ImageData object
blendMultipleData(images, algorithm, algorithms, isInline, options)

// benchmarks and prints to console various times for different combinations of blending algorithms, compression algorithms and filters for `blendImages`
blendImagesBenchmarkAll(bot, top, isInline)

// benchmarks and prints to console various times for different combinations of blending algorithms, compression algorithms and filters for `blendMultiple`
blendMultipleBenchmarkAll(imageFiles, isInline)

// returns a JSON of module constants (e.g. ALGORITHMS, FILTER_TYPES, COMPILER_VERSION, ...)
getModuleConstants()

Python 包

编译与执行

此 crate 可以通过使用 pip 作为 Python 包安装。只需运行

pip install .

用法

查看以下文件夹以获取示例: 此文件夹

使用以下命令导入 Python 包

import pconvert_rust

暴露的 Python API。参数 options 是可选参数的 Python 字典,如果指定了 num_threads 且值为 1 或更多,则工作负载将在多个线程之间分配(属于内部管理的线程池)。

# blends two images read from the local file system and writes the result to the file system
blend_images(bot_path, top_path, target_path, algorithm, is_inline, options)

# blends multiple images read from the local file system and writes the result to the file system
blend_multiple(img_paths, out_path, algorithm, algorithms, is_inline, options)

# returns a python dict with summary information about the internal thread pool (size, active jobs, queued jobs)
get_thread_pool_status()

# access module constants (e.g. ALGORITHMS, FILTER_TYPES, COMPILER_VERSION, ...)
pconvert_rust.ALGORITHMS
pconvert_rust.FILTER_TYPES
pconvert_rust.COMPILER_VERSION

测试

对于 rust crate 测试

cargo test

对于 Python API 测试

python setup.py test

针对WASM API测试

npm test

文档

使用以下方式生成文档

cargo doc --lib --all-features

许可证

P(NG)Convert Rust目前遵循Apache许可证,版本2.0

构建自动化

Build Status Build Status GitHub crates Status PyPi Status npm Status License

依赖项

~10–21MB
~117K SLoC