Skip to content

Using Photon Natively

Prerequisites

Add Photon as a dependency to your project's Cargo.toml:

Cargo.toml
photon-rs = "0.2.0"

Open an Image

To open an image:

bin.rs
1
2
3
4
5
extern crate photon_rs;
use photon_rs::native::{open_image};
fn main() {
    let mut img = open_image("image.jpg").expect("File should open");
}

Process The Image

To apply a filter effect to the opened image, we need to pass in our image and a filter name.

1
photon_rs::filters::filter(&mut img, "twenties");

Notice that we're passing a mutable reference to the image. This allows the function to modify the image, rather than return a new image. There are a variety of filter effects we can pass. Once you get the program compiled, try passing in "radio" instead of the filter above. For a full list, see the documentation.

Write to the Filesystem

Then, to write the image to the filesystem:

1
save_image(img, "new_image.jpg").expect("File should be saved");

Notice here we're saving it as a JPG image, but we could also save it as a PNG or a different output format, by including a different file extension.

Sample Program

This program adds a sepia effect to an image:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
extern crate photon_rs;
use photon_rs::{monochrome};
use photon_rs::native::{open_image, save_image};

fn main() {
    // Open the image (a PhotonImage is returned)
    let mut img = open_image("image.jpg").expect("File should open");

    // Apply a sepia effect to the image.
    monochrome::sepia(&mut img);

    save_image(img, "raw_image.png").expect("File should be saved");
}