Skip to content

Commit 0df9b3a

Browse files
committed
Fix image_to_pixels to handle non-RGB images
1 parent dc87009 commit 0df9b3a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mistralrs-vision/src/utils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ use image::{DynamicImage, GenericImageView};
44
/// Output is (c, h, w)
55
pub(crate) fn image_to_pixels(image: &DynamicImage, device: &Device) -> Result<Tensor> {
66
let (w, h) = image.dimensions();
7-
let data = image.to_rgb8().into_raw();
8-
let data = Tensor::from_vec(data, (h as usize, w as usize, n_channels(image)), device)?;
7+
let n_channels = n_channels(image);
8+
let data = match n_channels {
9+
1 => image.to_luma8().into_raw(),
10+
2 => image.to_luma_alpha8().into_raw(),
11+
3 => image.to_rgb8().into_raw(),
12+
4 => image.to_rgba8().into_raw(),
13+
_ => candle_core::bail!("Unsupported channel count {n_channels}"),
14+
};
15+
let data = Tensor::from_vec(data, (h as usize, w as usize, n_channels), device)?;
916
data.permute((2, 0, 1))?.to_dtype(DType::F32)
1017
}
1118

0 commit comments

Comments
 (0)