gpt4 book ai didi

image - 将&Vec RGB数据转换为ImageBuffer Rust

转载 作者:行者123 更新时间:2023-12-03 11:41:31 26 4
gpt4 key购买 nike

货代

image = "0.23.12"
fltk = "0.10.14"
我想使用rust的图像包装箱将rgb数据另存为jpeg文件:
use image::{RgbImage};
use image::ImageBuffer;
use fltk::{image::RgbImage, button::*};

let sourceImg = image::open("imgs/2.jpg").unwrap();
let rgb = RgbImage::new(&sourceImg.to_bytes(), x, y, 3).unwrap();
let mut prevWindowImgButton = Button::new(0,0, 300, 300, "");
prevWindowImgButton.set_image(Some(&rgb));

let rgbData= &prevWindowImgButton.image().unwrap().to_rgb_data();
//returns rgb data with type &Vec<u8>
rgbData.save("out/outputtest.jpg");
给出错误:
    testRGB.save("out/outputtest.jpg");
| ^^^^ method not found in `&Vec<u8>`
因为必须在ImageBuffer上使用.save。那么如何将rgb数据转换为ImageBuffer?

最佳答案

如果只想使用image crate 将原始缓冲区保存到图像文件,则可以使用save_buffer_with_format函数:

use image::io::Reader;
use image::save_buffer_with_format;

fn main() {
// The following three lines simply load a test image and convert it into buffer
let img = Reader::open("myimage.png").unwrap().decode().unwrap().to_rgb8();
let (width, height) = (img.width(), img.height());
let img_byte_vec = img.into_raw();
// The next line is what you want
save_buffer_with_format("myimg.jpg", &img_byte_vec, width, height, image::ColorType::Rgb8, image::ImageFormat::Jpeg).unwrap();
}

关于image - 将&Vec <u8> RGB数据转换为ImageBuffer Rust,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65066172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com