Here's a minimal code sample that should convert rgb
to rgba
image (goal: make white background transparent):
extern crate image;
fn main() {
// ```open``` returns a `DynamicImage` on success.
let img = image::open("image_with_white_background.png").unwrap();
// Should make white background transparent
// Found a similar question: https://stackoverflow.com/questions/765736/using-pil-to-make-all-white-pixels-transparent
// Is there a nice method for that loop (see the answer to the question above)?
let img = img.to_rgba();
img.save("image_with_transparent_background.png").unwrap();
}
It don't really work though, there's still white background in image_with_transparent_background.png
.
Here's the image I work with:
Update: this piece of code works, but I wonder if there's something more elegant (e.g., can I reuse existing library call instead):
fn main() {
let img = image::open("image_with_white_background.png").unwrap();
let mut img = img.to_rgba();
for p in img.pixels_mut() {
if p[0] == 255 && p[1] == 255 && p[2] == 255 {
p[3] = 0;
}
}
img.save("image_with_transparent_background.png").unwrap();
}