TIL: Base64 Encoding an ImageBuffer as PNG(in Rust)

• 1 min read

Recently, I had to base64-encode an image::ImageBuffer to send it as a data URL like

data:image/png;base64,iVBORw0KGgoAAAANSUhEU... 

Initially, I tried this:

// Cargo.toml
image = "0.24.7"
base64 = "0.21.5"
use base64::{engine::general_purpose, Engine as _};
use image::ImageBuffer;

// WRONG: doesn't base64-encode image as PNG
let image: ImageBuffer<Rgba<u8>, Vec<u8>> = ...;
let b64 = general_purpose::STANDARD.encode(bytes);
println!("b64: {:?}", b64);

But the ImageBuffer doesn’t store its pixels in PNG format, so the base64 string it returns won’t work inside a data URL.

Instead, we first need to create a separate buffer to write the image pixels as PNG, and then encode those bytes as base64:

use std::Cursor;

let image: ImageBuffer<Rgba<u8>, Vec<u8>> = ...;

let mut bytes: Vec<u8> = Vec::new();
image
    .write_to(&mut Cursor::new(&mut bytes), image::ImageOutputFormat::Png)
    .expect("Couldn't write image to bytes.");

let b64 = general_purpose::STANDARD.encode(bytes);
println!("b64: {:?}", b64);
Be the first to cheers
Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit

Hey 👋, thanks for reading!

I am currently looking for my next job.

If you know of someplace that might be a good fit for me, I'd really appreciate an intro: mail@samrat.me

Recommended Posts ✍🏻

See All »
• 6 min read
TIL: Sum Types With `instructor_ex`
Read Post »
• 1 min read
TIL: File Uploads Using the Req Elixir Library
Read Post »
• 1 min read
TIL: Creating `sentence-transformers` Embedding...
Read Post »