Svg / Png converter

I’m trying to build a SVG image with vg into a buffer, convert it to PNG, do some stuff over pixels and rebuild a new SVG from it, still into a buffer.


module type ImageManipulator : sig
  type svg
  type png 
  val svg_of_string : string -> svg
  val svg_of_png : png -> svg
  val png_of_svg : svg -> png
  val string_of_svg : svg -> string 

module I : ImageManipulator = struct .... end

let manipulate str =
  let svg = I.svg_of_string str in
  let png = I.png_of_svg svg in
  let png = (* Manipulation sur le type png *) in
  I.svg_of_png png |> I.string_of_svg

Is there an Ocaml library that allows to convert a PNG format to a SVG format into a buffer and vice versa (an equivalent of the ImageManipulator describes above)?

Not sure I understood what you wanted. But here’s an example on how to render a PNG inside a SVG image.

Thanks for your answer! This isn’t exactly what I’m trying to do.

I’m working on the learn-ocaml platform. My main goal is to compare two images generated with vg. To do so, I wanted to check the pixel matrixes, created thanks to cairo2 and build a third image, in grayscale, corresponding to the difference between the two images.

My problem is that I can’t save the generated image in any file: it must stay only in the ram memory. However, I need to get this image and insert it into the web page. I can’t insert some PNG code directly into the page without saving it into a file. The only way I’ve found is to get the SVG code from the PNG and insert this SVG into the html DOM.

I don’t know how I can convert the pixel matrix into a SVG code or if it exists a method to directly inject PNG content… Is there any way to do it ?

You should be able to this by base64 encoding the in-memory representation of the png and inject it via a data uri in the DOM see here.

1 Like

In fact the Vg image database does that so that you can download the various rendered examples by clicking on the surface, e.g. SVG, PDF.

That’s exactly what I was looking. Thanks so much for your help !