Question: Which reproducible process can enable Windows Python users to render a SVG image into PNG?
Many questions/answers (such as Convert SVG to PNG in Python and Server-side SVG to PNG (or some other image format) in python, which are not duplicates for the reasons explained below) explain how to convert a SVG to PNG with Python.
Unfortunately, none of them are ready-to-use for Python + Windows. After more than 20 minutes, and many different attempts, I'm still unable to do it.
More details about failing attempts:
Installing cairo on Windows is not straightforward, we have to use Gohlke's binaries Intalling pycairo with Python 3.7 on Windows :
pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
Even once cairo is installed, rsvg (from main answers of Server-side SVG to PNG (or some other image format) in python, Convert SVG to PNG in Python) is not available for Windows:
pip install rsvg # or pyrsvg
> ERROR: No matching distribution found for pyrsvg
Solutions with svglib or reportlab don't work out-of-the-box on Python3 + Windows:
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM
drawing = svg2rlg("a.svg")
renderPM.drawToFile(drawing, "file.png", fmt="PNG")
Indeed:
AttributeError: 'Image' object has no attribute 'fromstring'
So a solution - specific for Windows - would be helpful.
From the comments, the solution was to install svglib version 1.0.1 and reportlab 3.5.59.
Related
I have been using SVGLIB 0.8.1 for a few years now to incorporate existing color SVG chart images (created in R) into a PDF file. It has worked great.
When I try to upgrade to the latest SVGLIB 0.9.2, on a clean Windows machine with Python 3.6.1, my imported SVG images (whic are fine themselves) now appear in black and white once imported in the PDF file.
I also get this error when executing SVGLIB commands:
Unable to find a suitable font for 'font-family:Nimbus Sans L'
I really would like to upgrade SVGLIB. What am I doing wrong here?
Simply upgrading to SVGLIB 0.9.2 causes the process of importing SVG images into PDFs to break. Downgrading back to 0.8.1 returns everyuthing to normal.
from svglib.svglib import svg2rlg
my_canvas = canvas.Canvas(strLinuxMultiserverReportTemporaryFilename)
my_canvas.setPageSize((11.5*inch, 40*inch))
drawing = svg2rlg(image_path)
drawing.transform = (0.25,0,0,0.25,0,0)
intHeaderIndex = 2850
my_canvas.drawString(350, intHeaderIndex, 'System Performance Report')
renderPDF.draw(drawing, my_canvas, intCurrentColumnNumber, intCurrentRowNumber)
my_canvas.save()
I expect my nice color SVG files to be imported into the PDF file by SVGLIB and stay in color.
how to convert a gif image to webp in python, keeping it's animation.
from PIL import Image
im = Image.open('test.gif')
im.save('test.webp', 'webp', save_all=True)
get KeyError, is there any python solution?
I just skimmed through the documentation for PIL, which says clearly that webp format is supported. It however comes with a condition. The page states Only supported if the system webp library was built with webpmux support.
In order to continue, you will have to install the latest libwebp library for your corresponding OS. It is recommended to work with it provided you have Debian/Ubuntu OS. There are plenty of resources available on the net to help you.
You may have to reinstall PIL as well
Here is a related thread I came across GITHUB thread
I need to convert SVG with custom fonts into PNG, I’m using the following Python code for this.
import cairosvg
png = cairosvg.svg2png(bytestring=svg_data)
The code works fine on my local machine (running under Mac OS) with installed fonts. However, when I try to deploy it to remote Ubuntu instance the fonts somehow couldn't be loaded and I end up with PNG having some default fonts in it. The fonts are installed in the system (I've already tried different font folders, also fontconfig has the fonts in the list of available fonts) and the access rights seem to be in order. Any hints what could be going wrong will be useful!
Managed to solve the problem using Wand:
from wand.image import Image
with Image(blob=svg_data, format="svg") as image:
png_image = image.make_blob("png")
P.S. CairoSVG version still doesn't work.
I have built Python's PIL library from source (due to an error in the version installed from the installer executable) and can create Image() objects from jpg files on the build computer, however when I package this Python application using PyInstaller, the application is unable to open JPEG images. Have I not built PIL correctly, as described at PIL encoder jpeg not available, or is something else wrong?
Update: The error message is "IOError: encoder jpeg not available".
Edit: The problem with the version installed from the installer executable (for both versions 1.1.7 and 1.1.6, if not earlier ones, too) is that it links against the VC90.DebugCRT library/assembly, which I don't is meant to be distributed and which PyInstaller could not find on my system (though a version was there).
You need to include the jpeg encoder/decoder dll with your installation, otherwise it won't work on systems that doesn't already have it.
Haven't tried this myself, but you might also be able to do this by static linking instead by running the configure script with --enable-static.
You have 2 choices:
use PIL's prebuilt binaries from http://www.pythonware.com/products/pil/
build from source (Note you have to setup JPEG library before get JPEG support, it's not easy on windows)
References ( not necessary for solving your problem ):
"Prerequisites" of Pillow (a fork for PIL): https://pypi.python.org/pypi/Pillow/1.7.8#build-instructions-all-platforms
see "Attention" of oscar: http://django-oscar.readthedocs.org/en/latest/internals/getting_started.html
Please ensure that pillow, a fork of the the Python Imaging Library (PIL), gets installed with JPEG support. Supported formats are printed when pillow is first installed. Instructions on how to get JPEG support are highly platform specific, but guides for PIL should work for pillow as well. Generally speaking, you need to ensure that libjpeg-dev is installed and found during installation.
What is python-3 using instead of PIL for manipulating Images?
The "friendly PIL fork" Pillow works on Python 2 and 3. Check out the Github project for support matrix and so on.
Christoph Gohlke managed to build PIL (for Windows only) for python versions up to 3.3: http://www.lfd.uci.edu/~gohlke/pythonlibs/
I tried his version of PIL with Python 3.2, and image open/create/pixel manipulation/save all work.
Qt works very well with graphics. In my opinion it is more versatile than PIL.
You get all the features you want for graphics manipulation, but there's also vector graphics and even support for real printers. And all of that in one uniform API, QPainter.
To use Qt you need a Python binding for it: PySide or PyQt4.
They both support Python 3.
Here is a simple example that loads a JPG image, draws an antialiased circle of radius 10 at coordinates (20, 20) with the color of the pixel that was at those coordinates and saves the modified image as a PNG file:
from PySide.QtCore import *
from PySide.QtGui import *
app = QCoreApplication([])
img = QImage('input.jpg')
g = QPainter(img)
g.setRenderHint(QPainter.Antialiasing)
g.setBrush(QColor(img.pixel(20, 20)))
g.drawEllipse(QPoint(20, 20), 10, 10)
g.end()
img.save('output.png')
But please note that this solution is quite 'heavyweight', because Qt is a large framework for making GUI applications.
As of March 30, 2012, I have tried and failed to get the sloonz fork on GitHub to open images. I got it to compile ok, but it didn't actually work. I also tried building gohlke's library, and it compiled also but failed to open any images. Someone mentioned PythonMagick above, but it only compiles on Windows. See PythonMagick on the wxPython wiki.
PIL was last updated in 2009, and while it's website says they are working on a Python 3 port, it's been 3 years, and the mailing list has gone cold.
To solve my Python 3 image manipulation problem, I am using subprocess.call() to execute ImageMagick shell commands. This method works.
See the subprocess module documentation.
You can use my package mahotas on Python 3. It is numpy-based rather than PIL based.
You want the Pillow library, here is how to install it on Python 3:
pip3 install Pillow
If that does not work for you (it should), try normal pip:
pip install Pillow
Depending on what is needed, scikit-image may be the best choice, with manipulations going way beyond PIL and the current version of Pillow. Very well-maintained, at least as much as Pillow. Also, the underlying data structures are from Numpy and Scipy, which makes its code incredibly interoperable. Examples that pillow can't handle:
You can see its power in the gallery. This paper provides a great intro to it. Good luck!
If you are on Python3 you can also use the library PILasOPENCV which works in Python 2 and 3. Function api calls are the same as in PIL or pillow but internally it works with OpenCV and numpy to load, save and manipulate images. Have a look at https://github.com/bunkahle/PILasOPENCV or install it with pip install PILasOPENCV. Not all PIL functions have been simulated but the most common functions work.