How can I handle images using rust?

I would like to learn Rust and thought it would be fun to manipulate images. I don't know where to start ... piston / rust-image ? But even with that, where do I start?

+3


source to share


1 answer


First you need docs and a repository .

This is not immediately visible from the landing page of the documentation, but the main type in image

is ImageBuffer

.

The function allows you to build a representing image with a given / width, keeping the pixels of the given (eg RGB or with transparency ). You can use methods such as , and (the latter below in the documentation) to change the image. For example. new

ImageBuffer

pixels_mut

get_pixel_mut

put_pixel

pixels_mut



extern crate image;

use image::{ImageBuffer, Rgb};

const WIDTH: u32 = 10;
const HEIGHT: u32 = 10;

fn main() {
    // a default (black) image containing Rgb values
    let mut image = ImageBuffer::<Rgb<u8>>::new(WIDTH, HEIGHT);

    // set a central pixel to white
    image.get_pixel_mut(5, 5).data = [255, 255, 255];

    // write it out to a file
    image.save("output.png").unwrap();
}

      

which looks like this: output

The repo is especially useful as a starting point because it contains examples, in particular it is an example of programmatically generating an image . When I use a new library, I will open the docs and, if confused, the repo specifically to look for examples.

+4


source







All Articles