You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rust中无损裁剪PNG图片且避免色彩失真?

如何通过脚本无损裁剪PNG图片且避免色彩失真?

我为此问题困扰数日,尝试过Python和Rust中的多种图像编辑方法。看似简单,但找不到能在不导致全图色彩失真的情况下裁剪PNG图片的方式。

我运行了一段来源未知的Rust image crate示例脚本:

use image::GenericImageView;

fn main() {
    // Use the open function to load an image from a Path.
    // `open` returns a `DynamicImage` on success.
    let img = image::open("../test.png").unwrap();

    // The dimensions method returns the images width and height.
    println!("dimensions {:?}", img.dimensions());

    // The color method returns the image's `ColorType`.
    println!("{:?}", img.color());

    // Write the contents of this image to the Writer in PNG format.
    img.save("../test copy.png").unwrap();
}

生成的test copy.png比原图小约25%,且明显更暗。

我希望实现仅裁剪图片、不做其他修改的效果。手动用MS Paint很容易做到,但能否通过脚本实现?

编辑补充:已将PNG文件及失真副本上传至代码仓库。


内容的提问来源于stack exchange,提问作者Densaugeo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 11:02:04