Image: How can I draw a rectangle in image

Created on 24 Aug 2019  路  2Comments  路  Source: image-rs/image

I'm trying to open an image, converting it into gray, and drawing a rectangle, then save it under another name, so, I'm using image and imageproc, and wrote the below code:

use image::{GenericImageView, FilterType, Rgb};
use imageproc::drawing::draw_hollow_rect_mut;
use imageproc::rect::Rect;

fn main() {
    let img = image::open("self.jpg").unwrap();

    let mut gray = img.grayscale();
    let white = Rgb([255u8, 255u8, 255u8]);

    draw_hollow_rect_mut(&mut gray, Rect::at(60, 10).of_size(20, 20), white);

    gray.save("gray.png").unwrap();
}

But I got the below error:

error[E0277]: the trait bound `image::dynimage::DynamicImage: image::image::GenericImage` is not satisfied
  --> src/main.rs:34:5
   |
34 |     draw_hollow_rect_mut(&mut gray, Rect::at(60, 10).of_size(20, 20), white);
   |     ^^^^^^^^^^^^^^^^^^^^ the trait `image::image::GenericImage` is not implemented for `image::dynimage::DynamicImage`

   = note: required by `imageproc::drawing::rect::draw_hollow_rect_mut`
question

Most helpful comment

I've just published imageproc 0.19, which references image 0.22.1

All 2 comments

Seems to be a version issue: imageproc:0.18.0 pulls in image:0.21.3, whereas the latest version of image is 0.22.1. You can fix this by putting compatible versions in your Cargo.toml:

[dependencies]
image = "0.21.3"
imageproc = "0.18.0"

(The way you can figure out this sort of thing is to see that the docs say DynamicImage does implement GenericImage, suggesting a version mismatch, and then use cargo-tree to figure out exactly what the mismatch is.)

@HeroicKatora we should probably figure out a way to make sure that the latest versions of image and imageproc remain compatible so people don't keep running into this.

I've just published imageproc 0.19, which references image 0.22.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Horki picture Horki  路  7Comments

fintelia picture fintelia  路  8Comments

franleplant picture franleplant  路  3Comments

1.0
cryptoquick picture cryptoquick  路  5Comments

nabijaczleweli picture nabijaczleweli  路  6Comments