MCPcopy Create free account
hub / github.com/evilsocket/cake / image_preprocess

Function image_preprocess

cake-core/src/models/sd/sd.rs:699–717  ·  view source on GitHub ↗
(path: T)

Source from the content-addressed store, hash-verified

697}
698
699fn image_preprocess<T: AsRef<std::path::Path>>(path: T) -> Result<Tensor> {
700 let img = image::ImageReader::open(path)?.decode()?;
701 let (height, width) = (img.height() as usize, img.width() as usize);
702 let height = height - height % 32;
703 let width = width - width % 32;
704 let img = img.resize_to_fill(
705 width as u32,
706 height as u32,
707 image::imageops::FilterType::CatmullRom,
708 );
709 let img = img.to_rgb8();
710 let img = img.into_raw();
711 let img = Tensor::from_vec(img, (height, width, 3), &Device::Cpu)?
712 .permute((2, 0, 1))?
713 .to_dtype(DType::F32)?
714 .affine(2. / 255., -1.)?
715 .unsqueeze(0)?;
716 Ok(img)
717}
718
719#[cfg(test)]
720mod tests {

Callers 2

generate_imageMethod · 0.85

Calls 1

decodeMethod · 0.45

Tested by 1