Convert SVG to image - image

How do I convert an svg file to an image using Go ?
I found the amazing svgo library and would like to use it to generate a custom set of playing cards. The idea is to store the text and layout of a card in a text file and then read and process it with go. This would be a huge improvement of my current workflow where I use gimp to edit each individual card. The problem is that I need to have an image of the card for printing. Preferably png since the printing script so far only works with that format. But I could easily adapt it to accept jpeg, too.
Unfortunately svgo doesn't seem to offer export functionality. Can you recommend a go library to convert svg to png ?

One possible strategy is to write your SVG to files and invoke an external tool to convert them. For example, ImageMagick and its related GraphicsMagick will both convert SVG to PNG via command-line options. You would need to use the convert verb, possibly within their batch support if you're converting lots of images at once.
GraphicsMagick has bindings for C and Go and other languages that you could use directly from your Go scripts, although I've not tried this myself.

I can't find a native Go library to do it, but there seems to be a way to convert an HTML canvas element to PNG quite simply in Javascript.
You can therefore output SVG to an HTML canvas element, and then use JS to export to SVG.
See this answer for details.

Related

Select best image format using pandoc

Pandoc can easily render the same source file to html or LaTeX and then pdf. In many cases, I'd like to use different versions of the same image file depending on which backend is being used: When the original "image" is a vector figure, possibly containing text (e.g. PostScript, PDF, maybe SVG), I'd like to use that version to generate LaTeX and PDF, so there's not loss of quality. But browsers don't generally render those well, so I'd like to generate and use a raster (e.g. PNG) image when generating HTML.
Is there any way to do this in Pandoc? What I was hoping would "just work" was something like the \includegraphics{} LaTeX macro where, if you provide the filename with no extension, it identifies all the image files matching that stem and applies some heuristic to pick the best option.
You could call pandoc with --default-image-extension=png when generating html, and --default-image-extension=pdf (or whichever other extension you use) when generating the pdf. See
https://pandoc.org/MANUAL.html#reader-options

I have generated a pdf file using matplotlib and I want to add a logo to this pdf file. How can I do it

I am using matplotlib to draw a graph using some data and I have saved it in Pdf format.Now I want to add a logo to this file.How can I do this.
Thanks in advance
If you can do it the other way round, it is easier:
plot the image
load the logo from file with, e.g. Image module (PIL)
add the logo with plt.imshow, use the extent keyword to place it correctly
save the image into PDF
(You may even want to plot the logo first, so that it stays in the background.)
Unfortunately, this does not work with vector graphics, but as logos usually are not that large, you may use a .png or even a .jpg.
If you already have the PDF's then this is not a matplotlib or python question. You need some PDF editing tools or libraries to add the logo. Possible, but an entirely different thing.

Converting multiple Postscript images into an animated gif

I have several postscript images of the form "filename_0001.ps", "filename_0002.ps", etc. I would like to create an animated gif with each of the files as a frame in the animation, but I do not know how I would go about doing that. I have over 500 files that I would like to include in a single gif and would like to avoid creating the animated gif by hand.
I googled around for a bit and found this. At the very bottom it seems to suggest that there is a one line command that can be used to make the gif, but I haven't been able to figure out how to use it in my case.
Any suggestions would be appreciated.
I've used ghostscript and ImageMagick's convert to do this. I used these commands
gs -sDEVICE=png48 -g500x500 -o bezan%03d.png bezanim.ps
convert bezan*.png bezan.gif
to produce this animation for this Codegolf.Stackexchange challenge, where the frames were separate pages of a single postscript program.
You can probably do it with the single command
convert filename_*.ps anim.gif
and convert will shell-out to ghostscript as needed. But for my case, I wanted to specify the arguments to ghostscript directly.

Inkscape - Not fully converting png into svg

I opened one PNG file in Inkscape and exported it as SVG. When I opened that file with notepad I came to know that the PNG file is embedded within it. If the conversion happened then the resulting file should have only SVG related tags. It shouldn't embed the image within it. Or Am I doing anything wrong.
Note: Save as option also producing the same kind of file. I am using Inkscape version 0.48 in Windows 7 - 64 bit.
This is a bit of an old thread, but it comes up early in Google so I thought I'd contribute something.
In Inkscape, you must do a trace to change the image into SVG. Look at the Path | Trace bitmap menu item and play with the options on that screen.
After creating the trace, you can remove your source image and have a pure svg in your saved file.
I've found it helpful to create layers in Inkscape and move the source image to one layer and put the trace on another layer to let me make quick comparisons using the 'hide layer' buttons.
BTW, your source image can be anything - bmp, jpg, png, etc.
A .png file is a raster image file. In order to convert it to a vector graphic based format like .svg and have it be "native" svg rather than an included image you are going to either have to use a program that can rasterize it or in Inkscape trace the bitmap and turn it into paths. Inkscape provides information on tracing: http://inkscape.org/doc/tracing/tutorial-tracing.html

How can I store raw data in an image file?

I have some raw data in a file that I would like to store in an image file (bmp, jpg, png, or even gif (eegad)). I would like this to be a two way process: I need to be able to reliably convert the image file back later and get a file that is identical to the original file.
I am not looking for a how-to on steganography; the image file will probably be one pixel wide and millions of pixels high and look like garbage. That is fine.
I looked into the Imagemagick utility convert, but am intimidated by the large number of options and terse man page. I am guessing I could just use this to convert from a 'raw' black channel to png, but would have to specify a bunch of other stuff. Any hints? I would prefer to work within Imagemagick or using Linux utilities.
If you are wondering, there's nothing black hat or cloak and dagger about my request. I simply want to automatically backup some important data to a photo-sharing site.
I'd plow into ImageMagick if that's what you'd prefer anyway.
Specific image formats support storing text data to different degrees, and ImageMagick supports all of the formats you mentioned. I'd choose the one that lets you store what you need.

Resources