NOTE: I did try the Inkscape approaches as well and also had the pixelation problem, but had already seen an improvement with the density approach so I decided to dig into that deeper. Remember that this resizes the image as well and you will need to adjust for that when using / implementing the image as compared to the original svg. The original density was far too pixelated as a png for me, so in my case 5x the original density or -density 480 was good enough for me. Now that we have the right original density converted to dpi, the rest of the logic stated in the above answers falls into place and the svg file can be scaled to a better "resolution" by multiplying the original density. If you prefer a chart or online calculator for this you can try. Now we know the current density of the image but may need to convert it to the correct units for conversion or mogrifying (PixelsPerInch or dpi) This will put out a lot of metadata about the image file, including: Image:įor a more concise query you can try: identify -format "%x x %y %U" test.svg ImageMagick has a way to sort that out: identify -verbose test.svg We have seen 72 in this answer and 96 in this answer being suggested as the default density of an image, but which one? what if mine is different? So I decided to dig a little deeper and solve that problem as it seemed to be a side effect of this approach and I think it has to do with the original density or dpi. I was getting "low poly" curves using the general approach of increasing the density. Os.remove(os.path.join(tempPath, fileNameRoot + '.svg')) Os.remove(os.path.join(tempPath, fileNameRoot + '.jpg')) Jpg = open(os.path.join(tempPath, fileNameRoot + '.jpg'), 'rb').read() WriteToFile(os.path.join(tempPath, fileNameRoot + '.svg'), xmldoc.toxml())Įxecute('convert -background "%s" %s %s' % (backgroundColor, os.path.join(tempPath, fileNameRoot + '.svg'), os.path.join(tempPath, fileNameRoot + '.jpg'))) Xmldoc.getElementsByTagName("svg").attributes.value = '%spx' % newHeight Xmldoc.getElementsByTagName("svg").attributes.value = '%spx' % newWidth NewHeight = int(newWidth / width * height) Height = float(xmldoc.getElementsByTagName("svg").('px')) Width = float(xmldoc.getElementsByTagName("svg").('px')) TempPath = os.path.join(self.rootFolder, 'data')įileNameRoot = 'temp_' + str(image.getID()) Here's my Python code, a function that will return the JPG file's content: import gzip, re, osįrom ynlib.files import ReadFromFile, WriteToFileįrom import parse, parseStringĭef SVGToJPGInMemory(svgPath, newWidth, backgroundColor): I've solved this issue through changing the width and height attributes of the tag to match my intended output size and then converting it using ImageMagick. Magick -density "%" button.svg button3.png In ImageMagick 7, you can do the computation in-line as follows: magick -density "%" button.svg button3.png So you can compute the needed density in proportion to the ratios of the dimensions and the densities. The input is 50 at default density of 96 (older versions of Inkscape may be using 92). Here is a small 50x50 button when rendered at the default density of 96: convert button.svg button1.png There is no "magic" parameter that will do what you want.īut, one can compute very simply the exact density needed to render the output. If Inkscape is installed on the system, ImageMagick will use it automatically. RSVG is a delegate that needs to be installed with ImageMagick. In ImageMagick, one gets a better SVG rendering if one uses Inkscape or RSVG with ImageMagick than its own internal MSVG/XML rendered.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |