We can use draw_filled_rect_mut to draw a rectangle. The function needs a Rect and the color of the rectangle. Rect describes the top-left corner of the rectangle and its width and height. We use Rect::at to set the position and of_size to set the size.
use imageproc::{drawing, image, rect::Rect};
fn main() {
let mut buf = image::ImageBuffer::new(100, 100);
let rect = Rect::at(25, 25).of_size(50, 40);
drawing::draw_filled_rect_mut(&mut buf, rect, image::Rgb::from([128u8, 255u8, 64u8]));
buf.save("rect.png").unwrap();
}rect.png:
We can also only draw the border of the rectangle. This is done by draw_hollow_rect_mut.
To draw on a copied image, we can use draw_filled_rect and draw_hollow_rect.
➡️ Next: Drawing Polygons
📘 Back: Table of contents

