LoginSignup
3
1

More than 3 years have passed since last update.

RustでJPEG画像の各ピクセルのRGBを取得する

Posted at

絵でも描こうかと思っていたら, Rustでimageクレートをたたいていました。

0. 実行環境

  • Windows10 Home
  • rustc 1.39.0

1. 使用したクレート

  • image = "0.22.3"

2. ソースコード

main.rs
use image::open;

fn main() {
    let img = open("src/sample_image.jpg").unwrap().to_rgb();

    let img_width = img.width();    // 画像の幅を取得
    let img_height = img.height();  // 画像の高さを取得

    for y in 0..img_height {
        for x in 0..img_width {
            let pixel = img.get_pixel(x, y);   // (x, y)のピクセルを取得
            let r = pixel[0];
            let g = pixel[1];
            let b = pixel[2];
            println!("({}, {}, {})", r, g, b);
        }
    }
}
Cargo.toml
[package]
name = "image_practice"
version = "0.1.0"
authors = ["*** <miserarenaiyo@example.com>"]
edition = "20XX"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
image = "0.22.3"

3. 実行結果

(100, 100, 100)
(100, 100, 101)
(100, 100, 100)
.
.
.

上のように延々とRGBが出力されます。

4. 参考URL

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1