rust(50)-图像(3)

DynamicImage

DynamicImage是所有受支持的ImageBuffer

类型的枚举。它的确切图像类型是在运行时确定的。它是打开图像时返回的类型。为了方便,动态图像重新实现了所有的图像处理功能。
DynamicImage实现RGBA像素的一般图像特征。

SubImage
以矩形坐标为界的另一幅图像的视图。它用于在图像的子区域上执行图像处理功能。

extern crate image;

use image::{GenericImageView, ImageBuffer, RgbImage, imageops};

let mut img: RgbImage = ImageBuffer::new(512, 512);
let subimg = imageops::crop(&mut img, 0, 0, 100, 100);

assert!(subimg.dimensions() == (100, 100));

这些是在imageops模块中定义的函数。所有函数都对实现GenericImage trait.的类型进行操作。
blur: 模糊:对提供的图像执行高斯模糊。
brighten: 变亮:变亮提供的图像
huerotate:色调将提供的图像按程度旋转
contrast 对比度:调整提供的图像的对比度
crop:裁剪:将一个可变的视图返回到一个图像中
filter3x3:对提供的图像执行一个3x3框式过滤器。
flip_horizontal: 水平翻转:水平翻转图像
flip_vertical:将图像垂直翻转
grayscale: 灰度:将提供的图像转换为灰度
invert: 反转:反转提供的图像中的每个像素。
resize:调整大小:将提供的图像调整到指定的尺寸
rotate180:顺时针旋转图像180度。
rotate旋转270:顺时针旋转图像270度。
rotate90:顺时针旋转图像90度。
unsharpen: 未锐化:对提供的映像执行未锐化掩码

打开和保存图像
image提供用于从路径打开图像的open函数。图像格式由路径的文件扩展名决定。io模块提供了一个提供更多控制的阅读器。

extern crate image;

use image::GenericImageView;

fn main() {
    // Use the open function to load an image from a Path.
    // `open` returns a `DynamicImage` on success.
    let img = image::open("tests/images/jpg/progressive/cat.jpg").unwrap();

    // The dimensions method returns the images width and height.
    println!("dimensions {:?}", img.dimensions());

    // The color method returns the image's `ColorType`.
    println!("{:?}", img.color());

    // Write the contents of this image to the Writer in PNG format.
    img.save("test.png").unwrap();
}

生成的分形

//! An example of generating julia fractals.
extern crate image;
extern crate num_complex;

fn main() {
    let imgx = 800;
    let imgy = 800;

    let scalex = 3.0 / imgx as f32;
    let scaley = 3.0 / imgy as f32;

    // Create a new ImgBuf with width: imgx and height: imgy
    let mut imgbuf = image::ImageBuffer::new(imgx, imgy);

    // Iterate over the coordinates and pixels of the image
    for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
        let r = (0.3 * x as f32) as u8;
        let b = (0.3 * y as f32) as u8;
        *pixel = image::Rgb([r, 0, b]);
    }

    // A redundant loop to demonstrate reading image data
    for x in 0..imgx {
        for y in 0..imgy {
            let cx = y as f32 * scalex - 1.5;
            let cy = x as f32 * scaley - 1.5;

            let c = num_complex::Complex::new(-0.4, 0.6);
            let mut z = num_complex::Complex::new(cx, cy);

            let mut i = 0;
            while i < 255 && z.norm() <= 2.0 {
                z = z * z + c;
                i += 1;
            }

            let pixel = imgbuf.get_pixel_mut(x, y);
            let image::Rgb(data) = *pixel;
            *pixel = image::Rgb([data[0], i as u8, data[2]]);
        }
    }

    // Save the image as “fractal.png”, the format is deduced from the path
    imgbuf.save("fractal.png").unwrap();
}

在这里插入图片描述

写原始缓冲区
如果由于图像是通过其他方式获得的,因此不需要高级接口,则image提供save_buffer函数来将缓冲区保存到文件中。

extern crate image;

fn main() {

    let buffer: &[u8] = unimplemented!(); // Generate the image data

    // Save the buffer as "image.png"
    image::save_buffer("image.png", buffer, 800, 600, image::ColorType::Rgb8).unwrap()
}
发布了473 篇原创文章 · 获赞 14 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/105012027
50