gecko0307 / dlib

Allocators, I/O streams, math, geometry, image and audio processing for D
https://gecko0307.github.io/dlib
Boost Software License 1.0
217 stars 33 forks source link

Base64 to image #172

Closed aberba closed 4 years ago

aberba commented 4 years ago

Is there a way in dlib to convert a base64 image string (or decoded buffer) to a png or jpg format? I'm able to decode it to a buffer but turning a buffer into and Image is not something trivial without knowledge in image processing, I think.

gecko0307 commented 4 years ago

It can be done with std.base64 and ArrayStream:

import std.base64;
import dlib;

void main()
{
    string base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADklEQVR42mL4z8AAEGAAAwEBAGb9nyQAAAAASUVORK5CYII=";
    ubyte[] buffer = Base64.decode(base64);
    auto strm = new ArrayStream(buffer, buffer.length);
    SuperImage img = loadPNG(strm);
    img.savePNG("out.png");
}
aberba commented 4 years ago

Nice. Thanks.