#file-format #convert-file #format-conversion #file #format #convert #image-format

converter_buddy

文件格式转换库。提供不同格式文件间的转换工具。

4 个版本

0.2.1 2023年4月24日
0.2.0 2023年4月21日
0.1.2 2022年7月3日
0.1.1 2022年6月20日
0.1.0 2022年6月18日

#476 in 图像

MIT 许可证

74KB
1.5K SLoC

Converter Buddy

Converter Buddy 提供了一种简单易用的方式来转换文件格式。

目前仅支持最流行的图像格式,但目标是扩展到文档、音频和视频格式。

基本用法

ConvertibleFile 是 std::fs::File 的转换工具包装器

let src_path = "tests/assets/test.png";

let file = ConvertibleFile::new(src_path);
let format = file.format().expect("No format found");
let target_format = Format::Jpeg;

println!("Found source format: {}", format);
println!("Converting to {} format", target_format);

match file.convert(target_format) {
    Ok(_) => println!("Conversion successful"),
    Err(e) => println!("Conversion failed: {:?}", e),
}

如果您想使用字节数组而不是 std::fs 原语,可以使用底层的转换器

fn get_input_data() -> Vec<u8> {
    ...
}

fn main() {
    let input = get_input_data();
    let mut output = Vec::<u8>::new();

    PngConverter.process(&input, &mut output, JpegConfig::default()).expect("Conversion error");
    
    // or in a more generic way
    let source_format = Format::Png;
    let target_format = Format::Jpeg;

    let converter = Converter::try_from(source_format).expect("This format cannot be converted");
    converter.process(&input, &mut output, target_format.into()).expect("Conversion error");

    // use output ...
}

兼容性

从/到 PNG JPEG BMP TIFF GIF SVG WEBP PDF
PNG
JPEG
BMP
TIFF
GIF
SVG
WEBP

依赖项

~20MB
~177K SLoC