etemesi254 / zune-image

A fast and memory efficient image library in Rust
Other
319 stars 29 forks source link

Python `zil.Image.resize()` should be implemented #232

Closed kiyoon closed 1 week ago

kiyoon commented 1 week ago

It seems like with zune-python, it's impossible to resize the image. I feel this is one of the most fundamental features that should be implemented.

kiyoon commented 1 week ago

I implemented this on my own.

In py_image.rs,

use zune_imageprocs::resize::{Resize, ResizeMethod as ZResizeMethod};

use crate::py_enums::{
    ColorSpace, ImageDepth, ImageFormat, ImageThresholdType, ResizeMethod, ZImageErrors
};

// ...
    pub fn resize(
        &mut self, new_width: usize, new_height: usize, method: ResizeMethod, in_place: bool
    ) -> PyResult<Option<Image>> {
        exec_filter(
            self,
            Resize::new(new_width, new_height, method.to_resizemethod()),
            in_place
        )
    }

In py_enums.py

use zune_imageprocs::resize::ResizeMethod as ZResizeMethod;

#[pyclass]
#[derive(Copy, Clone)]
pub enum ResizeMethod {
    Bilinear,
    Bicubic
}

impl ResizeMethod {
    pub(crate) fn to_resizemethod(self) -> ZResizeMethod {
        match self {
            ResizeMethod::Bilinear => ZResizeMethod::Bilinear,
            ResizeMethod::Bicubic => ZResizeMethod::Bicubic
        }
    }
}

impl From<ZResizeMethod> for ResizeMethod {
    fn from(value: ZResizeMethod) -> Self {
        match value {
            ZResizeMethod::Bilinear => ResizeMethod::Bilinear,
            ZResizeMethod::Bicubic => ResizeMethod::Bicubic
        }
    }
}

In lib.rs

use crate::py_enums::{ColorSpace, ImageDepth, ImageFormat, ImageThresholdType, ResizeMethod};

// ...
    m.add_class::<ResizeMethod>()?;

However, I had to modify the non-head commit because the recent one wouldn't work (#231).

I can submit a PR once the issue gets resolved.

etemesi254 commented 1 week ago

Please go ahead and submit a PR

The issue in open has been resolved

kiyoon commented 1 week ago

How is formatting done in this project? I've used the rust-analyzer's formatting (I believe it's identical to rustfmt), and it adds numerous changes, usually with the trailing commas.

diff --git a/crates/zune-python/src/py_enums.rs b/crates/zune-python/src/py_enums.rs
index 87b354f6..2dd1e288 100644
--- a/crates/zune-python/src/py_enums.rs
+++ b/crates/zune-python/src/py_enums.rs
@@ -11,11 +11,12 @@ use zune_core::bit_depth::BitDepth;
 use zune_core::colorspace::ColorSpace as ZColorSpace;
 use zune_image::codecs::ImageFormat as ZImageFormat;
 use zune_image::errors::ImageErrors;
+use zune_imageprocs::resize::ResizeMethod as ZResizeMethod;
 use zune_imageprocs::threshold::ThresholdMethod;

 #[pyclass]
 pub struct ZImageErrors {
-    pub(crate) error: zune_image::errors::ImageErrors
+    pub(crate) error: zune_image::errors::ImageErrors,
 }

 impl From<ImageErrors> for ZImageErrors {
@@ -38,7 +39,7 @@ pub enum ImageFormat {
     Qoi,
     JPEG_XL,
     HDR,
-    Unknown
+    Unknown,
 }

 impl ImageFormat {
@@ -53,7 +54,7 @@ impl ImageFormat {
             ImageFormat::Qoi => ZImageFormat::QOI,
             ImageFormat::JPEG_XL => ZImageFormat::JPEG_XL,
             ImageFormat::HDR => ZImageFormat::HDR,
-            ImageFormat::Unknown => ZImageFormat::Unknown
+            ImageFormat::Unknown => ZImageFormat::Unknown,
         }
     }
     /// Return true if an image format has an encoder
@@ -80,7 +81,7 @@ impl From<ZImageFormat> for ImageFormat {
             ZImageFormat::JPEG_XL => ImageFormat::JPEG_XL,
             ZImageFormat::HDR => ImageFormat::HDR,
             ZImageFormat::BMP => ImageFormat::BMP,
-            _ => ImageFormat::Unknown
+            _ => ImageFormat::Unknown,
         }
     }
 }
@@ -100,7 +101,7 @@ pub enum ColorSpace {
     CMYK,
     Unknown,
     HSL,
-    HSV
+    HSV,
 }

 impl ColorSpace {
@@ -116,7 +117,7 @@ impl ColorSpace {
             ColorSpace::CMYK => ZColorSpace::CMYK,
             ColorSpace::HSL => ZColorSpace::HSL,
             ColorSpace::HSV => ZColorSpace::HSV,
-            ColorSpace::Unexposed | ColorSpace::Unknown => ZColorSpace::Unknown
+            ColorSpace::Unexposed | ColorSpace::Unknown => ZColorSpace::Unknown,
         }
     }
 }
@@ -135,7 +136,7 @@ impl From<ZColorSpace> for ColorSpace {
             ZColorSpace::BGRA => ColorSpace::BGRA,
             ZColorSpace::HSL => ColorSpace::HSL,
             ZColorSpace::HSV => ColorSpace::HSV,
-            _ => ColorSpace::Unknown
+            _ => ColorSpace::Unknown,
         }
     }
 }
@@ -146,7 +147,7 @@ pub enum ImageDepth {
     U8,
     U16,
     F32,
-    Unknown
+    Unknown,
 }

 impl ImageDepth {
@@ -155,7 +156,7 @@ impl ImageDepth {
             ImageDepth::U8 => BitDepth::Eight,
             ImageDepth::U16 => BitDepth::Sixteen,
             ImageDepth::F32 => BitDepth::Float32,
-            ImageDepth::Unknown => BitDepth::Unknown
+            ImageDepth::Unknown => BitDepth::Unknown,
         }
     }
 }
@@ -166,7 +167,7 @@ impl From<BitDepth> for ImageDepth {
             BitDepth::Eight => ImageDepth::U8,
             BitDepth::Sixteen => ImageDepth::U16,
             BitDepth::Float32 => ImageDepth::F32,
-            _ => ImageDepth::Unknown
+            _ => ImageDepth::Unknown,
         }
     }
 }
@@ -178,7 +179,7 @@ pub enum ImageThresholdType {
     Binary,
     BinaryInv,
     ThreshTrunc,
-    ThreshToZero
+    ThreshToZero,
 }

 impl ImageThresholdType {
@@ -187,7 +188,31 @@ impl ImageThresholdType {
             ImageThresholdType::Binary => ThresholdMethod::Binary,
             ImageThresholdType::BinaryInv => ThresholdMethod::BinaryInv,
             ImageThresholdType::ThreshTrunc => ThresholdMethod::ThreshTrunc,
-            ImageThresholdType::ThreshToZero => ThresholdMethod::ThreshToZero
+            ImageThresholdType::ThreshToZero => ThresholdMethod::ThreshToZero,
+        }
+    }
+}
+#[pyclass]
+#[derive(Copy, Clone)]
+pub enum ResizeMethod {
+    Bilinear,
+    Bicubic,
+}
+
+impl ResizeMethod {
+    pub(crate) fn to_resizemethod(self) -> ZResizeMethod {
+        match self {
+            ResizeMethod::Bilinear => ZResizeMethod::Bilinear,
+            ResizeMethod::Bicubic => ZResizeMethod::Bicubic,
+        }
+    }
+}
+
+impl From<ZResizeMethod> for ResizeMethod {
+    fn from(value: ZResizeMethod) -> Self {
+        match value {
+            ZResizeMethod::Bilinear => ResizeMethod::Bilinear,
+            ZResizeMethod::Bicubic => ResizeMethod::Bicubic,
         }
     }
 }
etemesi254 commented 1 week ago

It's okay, you can submit, formatting will be sorted out later