Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a line in a bitmap (possibly with piston)

Tags:

rust

I want to draw a line in a bitmap, e.g. from pixel (10, 10) to pixel (90, 90). The line must have a specific width.

Using piston image, I am able to draw a single pixel:

let mut image = ImageBuffer::<image::Rgb<u8>>::new(100, 100);
image.get_pixel_mut(5, 5).data = [255, 255, 255];
image.save("output.png");

However there is no method to draw a line.

I suppose I have to use piston::graphics for that, but I can’t find any ressource how to do it (any example involves a window that provides a context on which graphics works on).

like image 831
Tristram Gräbener Avatar asked Mar 14 '23 19:03

Tristram Gräbener


1 Answers

In addition to the great answer above: there is now direct support for drawing lines and many more shapes (even texts) in the imageproc library (see also the examples there):

extern crate image;
extern crate imageproc;

use image::{Rgb, RgbImage};
use imageproc::drawing::draw_line_segment_mut;

fn main() {
    let mut img = RgbImage::new(100, 100);
    draw_line_segment_mut(
        &mut img,
        (5f32, 5f32),              // start point
        (95f32, 95f32),            // end point
        Rgb([69u8, 203u8, 133u8]), // RGB colors
    );
    img.save("output.png").unwrap();
}
like image 153
Michael Avatar answered Mar 23 '23 16:03

Michael