Converting image files to svg?

Is there a way to convert image files to .svg? FileIO.jl seems to cover many of the file types I have (wmf, eps, png, bmp, bdf, etc.) with load and save. Any other package I can use? E.g., via savefig in Plots?

1 Like

My guess is no. I believe SVG is a vector format, meaning the “image” is a series of vectors. PNG, BMP, JPG, etc are bitmaps, i.e a each pixel has a color. Converting from pixels into vectors would be tricky and error prone. So you can probably go from SVG to the other image formats…but the reverse is no straight forward.

2 Likes

Yes, SVG is a vector format. But SVG files can also contain bitmapped images. I can open many bitmapped image formats in Inkscape, and save them as SVG files.

The main reason I want to convert bitmapped images to SVG is that my word processor (LyX) seems to handle SVG files better than some of the other formats. [I recently imported some bitmapped images into LyX, and it didn’t read properly the size of the images --> all images exceeded their boundaries into other images, way above the header, etc., and I had a major job to fix it.] So I was kind of thinking of using Julia to batch convert the files to SVG…

Maybe just create wrapper .svg files, each containing a single <image> tag? https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image

This could be done with a simple println statement from Julia.

1 Like

Even if you just want to embed a pixel image in an SVG, is there a specific reason you want to do this in Julia? There are many perfectly fine command line tools for these purposes, eg

1 Like

I guess it is the “dream” of a one-language-fits-all. In particular when I need to do batch conversion.

In this specific case, some figures were drawn in Visio, and saved as wmf, eps, pdf, etc. files to try to make the result look nice. Others are mobile phone pictures (bmp, png, etc.) of hand drawings meant for later drawing in Inkscape. Etc.

So I have multiple versions of each file. For some files, I even have svg files in addition to visio files, pdf files, etc. So I need to read all files. For file names where there already is an svg file, I don’t want to overwrite that file. For files where there is no svg file, I’d want to use a pdf file (or Visio file) if such exists. If only bitmap files exist, I’d make a ranking of which has best quality.

Anyway, I’ll take a look at ImageMagick – thanks for suggestion.

I am not sure this is a reasonable expectation.

Converting images is a very special task yet well-defined task, so it may not make sense for all languages to develop code for all formats out there (and there are very many).

Tasks like this are not where Julia shines; you would just replace a one-liner (eg calling out to the shell) with another one-liner.

You are probably right. I may take a look at PythonMagick.

Aha! Plots uses ImageMagick for loading. Maybe also for saving? The following works for me:

load("testimage.png") |> plot
savefig("testimage.svg")