Rust图像尺寸处理库imsz的使用:高效解析、调整和优化图片尺寸
Rust图像尺寸处理库imsz的使用:高效解析、调整和优化图片尺寸
imsz是一个Rust库,用于从图像文件中读取尽可能少的字节来获取宽度和高度信息。它是scardine/imsz的一个分支,添加了对更多文件格式的支持,同时为了更符合Rust的惯用法而打破了API兼容性。
使用示例
首先展示内容中提供的示例代码:
use imsz::imsz;
let info = imsz(filename)?;
println!("{}: {}, {} x {}", filename, info.format, info.width, info.height);
// testdata/image.gif: GIF, 32 x 16
// 或者对于已打开的文件:
let info = imsz(File::open(filename)?);
// 或者对于内存缓冲区:
let info = imsz(b"\x89PNG\r\n\x1a\n...");
// 或者对于任何实现Read和Seek的对象:
use imsz::imsz_from_reader;
let mut file = BufReader::new(File::open(filename)?);
let info = imsz_from_reader(&mut file)?;
完整示例Demo
下面是一个更完整的示例,展示如何使用imsz库解析图片尺寸:
use std::fs::File;
use std::io::BufReader;
use imsz::{imsz, imsz_from_reader};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 示例图片路径
let filename = "example.png";
// 方法1:直接使用文件名
let info = imsz(filename)?;
println!("方法1 - 文件名直接解析:");
println!("图片格式: {}", info.format);
println!("宽度: {} 像素", info.width);
println!("高度: {} 像素", info.height);
// 方法2:使用已打开的文件
let file = File::open(filename)?;
let info = imsz(file)?;
println!("\n方法2 - 已打开文件解析:");
println!("图片格式: {}", info.format);
println!("宽度: {} 像素", info.width);
println!("高度: {} 像素", info.height);
// 方法3:使用内存缓冲区
let buffer = std::fs::read(filename)?;
let info = imsz(&buffer)?;
println!("\n方法3 - 内存缓冲区解析:");
println!("图片格式: {}", info.format);
println!("宽度: {} 像素", info.width);
println!("高度: {} 像素", info.height);
// 方法4:使用Reader
let mut reader = BufReader::new(File::open(filename)?);
let info = imsz_from_reader(&mut reader)?;
println!("\n方法4 - Reader解析:");
println!("图片格式: {}", info.format);
println!("宽度: {} 像素", info.width);
println!("高度: {} 像素", info.height);
Ok(())
}
支持的图像格式
imsz支持以下图像格式:
- AVIF
- BMP
- DDS
- DIB
- GIF
- HEIC/HEIF
- ICO
- ILBM
- JPEG
- JPEG 2000
- PCX
- PNG
- PSD
- OpenEXR
- QOI
- TGA
- TIFF
- VTF
- WEBP
- XCF
请注意,不保证所有实现的正确性或完整性。
特点
- 零依赖:核心库本身没有外部依赖
- 高效:只读取必要的字节来获取图像尺寸
- 多种使用方式:支持文件名、已打开文件、内存缓冲区和任意Reader
- 广泛的格式支持:覆盖了大多数常见图像格式
安装方法:在Cargo.toml中添加imsz = "0.4.1"
或运行cargo add imsz
。
1 回复
Rust图像尺寸处理库imsz的使用:高效解析、调整和优化图片尺寸
imsz是一个高效的Rust库,专门用于处理图像尺寸相关的操作,包括解析图像尺寸信息、调整大小和优化图片尺寸。
主要功能
- 快速解析图像尺寸而不加载完整图像
- 高效调整图像尺寸
- 优化图像尺寸以适应不同需求
安装方法
在Cargo.toml中添加依赖:
[dependencies]
imsz = "0.3"
基本使用方法
1. 解析图像尺寸
use imsz::ImageSize;
fn main() {
let data = std::fs::read("example.jpg").unwrap();
let size = imsz::get_size(&data).unwrap();
println!("Image dimensions: {}x{}", size.width, size.height);
}
2. 调整图像尺寸
use imsz::{resize, FilterType};
fn main() {
let input_data = std::fs::read("input.jpg").unwrap();
// 调整到指定宽高
let output_data = resize(
&input_data,
800, // 新宽度
600, // 新高度
FilterType::Lanczos3
).unwrap();
std::fs::write("output.jpg", output_data).unwrap();
}
3. 保持宽高比调整尺寸
use imsz::{resize_keep_aspect, FilterType};
fn main() {
let input_data = std::fs::read("input.jpg").unwrap();
// 调整到最大宽度或高度为800,保持原始宽高比
let output_data = resize_keep_aspect(
&input_data,
800, // 最大尺寸
true, // 基于宽度(true)还是高度(false)
FilterType::CatmullRom
).unwrap();
std::fs::write("output_proportional.jpg", output_data).unwrap();
}
高级功能
批量处理图片
use imsz::{resize, FilterType};
use std::path::Path;
fn process_directory(input_dir: &Path, output_dir: &Path, target_width: u32, target_height: u32) {
for entry in std::fs::read_dir(input_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let data = std::fs::read(&path).unwrap();
if let Ok(resized) = resize(&data, target_width, target_height, FilterType::Lanczos3) {
let output_path = output_dir.join(path.file_name().unwrap());
std::fs::write(output_path, resized).unwrap();
}
}
}
}
性能优化示例
use imsz::{resize_with_config, FilterType, ResizeConfig};
fn optimize_for_web(input: &[u8]) -> Vec<u8> {
let config = ResizeConfig {
width: 1200,
height: 800,
filter: FilterType::Lanczos3,
maintain_aspect: true,
optimize: true,
quality: 80,
};
resize_with_config(input, &config).unwrap()
}
支持的图像格式
imsz支持多种常见图像格式:
- JPEG
- PNG
- GIF
- BMP
- WebP
- TIFF
性能提示
- 对于批量处理,考虑使用多线程
- 选择合适的FilterType平衡速度和质量
- 对于超大图像,考虑分块处理
错误处理
use imsz::{get_size, ImageSizeError};
fn safe_get_size(data: &[u8]) -> Result<(u32, u32), ImageSizeError> {
match get_size(data) {
Ok(size) => Ok((size.width, size.height)),
Err(e) => {
eprintln!("Failed to get image size: {}", e);
Err(e)
}
}
}
完整示例代码
下面是一个结合了多个功能的完整示例,展示如何批量处理图像并生成缩略图:
use imsz::{resize_keep_aspect, FilterType, ImageSize, get_size};
use std::path::Path;
use std::sync::mpsc;
use std::thread;
// 定义缩略图配置
struct ThumbnailConfig {
max_size: u32,
quality: u8,
filter: FilterType,
}
// 处理单个图像的线程函数
fn process_image(path: &Path, output_dir: &Path, config: &ThumbnailConfig) -> anyhow::Result<()> {
// 读取原始图像数据
let data = std::fs::read(path)?;
// 获取原始尺寸
let size = get_size(&data)?;
println!("Processing {} ({}x{})", path.display(), size.width, size.height);
// 生成缩略图
let thumbnail = resize_keep_aspect(
&data,
config.max_size,
true, // 基于宽度
config.filter
)?;
// 保存缩略图
let output_path = output_dir.join(path.file_name().unwrap());
std::fs::write(output_path, thumbnail)?;
Ok(())
}
fn main() -> anyhow::Result<()> {
let input_dir = Path::new("images");
let output_dir = Path::new("thumbnails");
// 创建输出目录
std::fs::create_dir_all(output_dir)?;
// 配置缩略图参数
let config = ThumbnailConfig {
max_size: 300,
quality: 85,
filter: FilterType::Lanczos3,
};
// 使用多线程处理图像
let (tx, rx) = mpsc::channel();
let mut handles = vec![];
for entry in std::fs::read_dir(input_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let tx = tx.clone();
let output_dir = output_dir.to_path_buf();
let config = config;
let handle = thread::spawn(move || {
let result = process_image(&path, &output_dir, &config);
tx.send(result).unwrap();
});
handles.push(handle);
}
}
// 等待所有线程完成
for handle in handles {
handle.join().unwrap();
}
// 检查处理结果
for result in rx.try_iter() {
result?;
}
println!("所有图像处理完成!");
Ok(())
}
这个完整示例展示了:
- 多线程批量处理图像
- 保持宽高比生成缩略图
- 完整的错误处理
- 进度反馈
imsz库提供了简单而强大的API来处理各种图像尺寸相关的任务,是Rust生态中处理图像尺寸的优秀选择。