Reputation: 3411
Cargo.toml
image = "0.23.12"
fltk = "0.10.14"
I'd like to save a rgb data as a jpeg file using rust's image crate:
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");
Gives error:
testRGB.save("out/outputtest.jpg");
| ^^^^ method not found in `&Vec<u8>`
Because .save must be used on an ImageBuffer. So how do I convert this rgb data to an ImageBuffer?
Upvotes: 0
Views: 2423
Reputation: 475
If all you want is to save a raw buffer to image file using the image
crate you can use the save_buffer_with_format
func:
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();
}
Upvotes: 5