Photon
repository·master·Indexed 26 days ago
https://github.com/silvia-odwyer/photonA high-performance, cross-platform image processing library written in Rust (photon-rs v0.3.3). It supports native use, Node.js, and the browser via WebAssembly. The library provides high-level filters and low-level pixel/channel manipulation across modules including effects, transform, correction, and convolution. Supported image formats include PNG, JPEG, BMP, ICO, TIFF, and WEBP.
What's inside photon
- Photon is a high-performance image processing library written in Rust and compilable to WebAssembly. It is designed for use both natively and on the web. It provides over 80 effects, including transformations, filters, channel manipulation, special effects, and color correction. It is optimized for speed, outperforming libraries like ImageMagick and the Python Imaging Library in various benchmarks.
Test against a locally compiled Photon crate
masterIf you want to test the React demo against a custom or locally compiled version of the Photon Rust crate:
- Navigate to the
crate/directory. - Run
wasm-pack buildto generate the WebAssembly package. - Point the demo at the generated
crate/pkgoutput (for example, usingnpm link).
- Navigate to the
Build the WebAssembly package
masterTo build the WebAssembly version of Photon, you must have
wasm-packinstalled. Run the following command from the repository root to build the crate.wasm-pack build ./crateRun the Photon Webpack Demo
masterTo run the pre-configured Webpack 5 and TypeScript demo locally for development, clone the repository, install dependencies, and start the development server. The server supports hot reloads for
src/index.tsand assets, serving the project athttp://localhost:8080.git clone https://github.com/silvia-odwyer/photon cd photon npm install npm run devUse Photon React Demo scripts
masterThe
react_app_demopackage provides several npm scripts for development, building, and linting:npm run dev: Starts the development server athttp://localhost:5173with hot module replacement.npm run build: Generates an optimized production bundle in thedist/directory and runs the TypeScript compiler.npm run preview: Serves the local production bundle for verification.npm run lint: Runs TypeScript type-checking (tsc --noEmit) without generating build artifacts.
npm run dev npm run build npm run preview npm run lintConfigure dependencies for JSON serialization
masterTo enable JSON serialization/deserialization for
PhotonImage, add the following to yourCargo.tomlfile:[dependencies] photon="0.0.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0"Run Photon examples from source
masterTo run the built-in examples to see Photon in action:
- Clone the repository.
- Navigate to the
cratedirectory. - Run the binary in release mode.
git clone https://github.com/silvia-odwyer/photon cd crate cargo run --releaseInstall Photon for Node.js
masterTo use Photon in a Node.js environment via WebAssembly, install the
@silvia-odwyer/photon-nodepackage using npm.npm install @silvia-odwyer/photon-nodeInstall Photon via npm
masterTo use Photon in a web project, install the package using npm:
npm install @silvia-odwyer/photonProcess images in the browser using WebAssembly
masterTo use Photon in a web environment, you must bridge the gap between browser
ImageDataand the Rust-basedPhotonImage.- Draw your image onto a
<canvas>element. - Use
module.open_image(canvas, ctx)to convert the canvas data into aPhotonImagethat the Rust library can process. - Call the desired WASM-friendly processing function (e.g.,
module.filter) on thePhotonImage. - Use
ctx.putImageData(rust_image, 0, 0)to place the modified pixels back onto the canvas.
Note: Only functions annotated with
#[wasm_bindgen]in the Rust core are available in the WebAssembly module.function filterImage(event) { // Create a canvas and get a 2D context from the canvas var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // Draw the image element onto the canvas ctx.drawImage(newimg, 0, 0); // Convert the ImageData found in the canvas to a PhotonImage (so that it can communicate with the core Rust library) let rust_image = module.open_image(canvas, ctx); // Filter the image, the PhotonImage's raw pixels are modified module.filter(rust_image, "radio"); // Place the pixels back on the canvas ctx.putImageData(rust_image, 0, 0) }- Draw your image onto a
Add Photon as a Rust dependency
masterTo use Photon in a native Rust project, add
photon-rsto yourCargo.tomlfile under the[dependencies]section.[dependencies] photon-rs="0.2.0"Run Photon native examples
masterTo run Photon examples natively using Rust, clone the repository and use
cargo run. Ensure you use the--releaseflag for performance. Outputted images are saved to theexample_outputdirectory.To run the default
example.rs:cargo run --example example --releaseTo run the text addition example:
cargo run --example add_textcargo run --example example --release