I am trying to use rsvg in Python 3.2 but I keep getting an import error. I have installed all of the librsvg packages along with cairo. I cannot find anything online about what else to install to get it to work. I did hear that the rsvg module hasn't been updated since 2005 so is it just not compatible with Python 3.2, or is there something else I can try to install it? Alternatively, if rsvg does not work, does anyone have any suggestions for a simple way to display an SVG file through Python (basically just show the image)?
EDIT: The error I get is: 'ImportError: No module named rsvg'
This error does not show in python2
Thanks in advance
I experienced a lot of difficulty trying to figure out how to do this. I hope others find this answer and save themselves a lot of time!
For Python 3, Python language bindings for several libraries originally written in C (including GTK, Clutter, and librsvg) have been replaced by GObject introspection libraries, Python code which dynamically generates Python objects from C "objects".
In order to use librsvg on Python 3, first install the necessary GObject introspection libraries (in addition to the Python 3 Cairo library). For example, on Ubuntu 13.10:
sudo apt-get install gir1.2-rsvg-2.0 python3-cairo python-gi-cairo python3-gi
Then test it out with the following code.
#!/usr/bin/env python3
# `gi.repository` is a special Python package that dynamically generates objects
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
import cairo
INPUTFILE = 'tiger.svg'
if __name__ == '__main__':
# create the cairo context
surface = cairo.SVGSurface('myoutput.svg', 580, 530)
context = cairo.Context(surface)
# use rsvg to render the cairo context
handle = Rsvg.Handle()
svg = handle.new_from_file(INPUTFILE)
svg.render_cairo(context)
In order to implement this for your project,
change cairo.SVGSurface to be whatever surface you are going to draw on, and
modify the value of INPUTFILE to be the name of the SVG file you wish to render.
Related
I was trying to do some work in Python with simulating 3D sound. I was trying to run this code (provided in the answer): Python openAL 3D sound and similar, both times receiving:
ModuleNotFoundError: No module named 'openal.audio'
I've installed OpenAL, PyAL, and tried installing an older version (uninstalled it and reinstalled the new one), but it still doesn't work.
I've also tried the following code:
from openal import *
class AudioSource:
def __init__(self, path_to_file):
self.src = oalOpen(path_to_file)
def play(self):
self.src.play()
self.src.position = (-100, 0, 0)
self.src.update()
which doesn't use the openal.audio package, but the position doesn't seem to have any effect on the sound source either. I'm open to any solution (including using a newer package if one is available that will produce 3D sound)
Using link to python OpenAL library at https://bitbucket.org/marcusva/py-al/downloads/
just download either the tar.gz or other one then once expanded it gives you dir something like
~/src/PyAL-0.1.0/
then type
make
if no errors there to install issue
make install
sudo make install # this works on ubuntu ... dunno about Windows or osx
then to kick tires run the provided example
cd ~/src/PyAL-0.1.0/examples/
python ./player.py
which is a rather nice 3D audio demo ... now your python import openal will work ... I confirmed this by running another script at https://stackoverflow.com/a/40945609/147175
If this still goes pear shaped what OS are you on ?
I run a python application which uses a glade builder file for its GUI.
I recently migrated to a KDE desktop (Debian Wheezy). After installing the Gtk bindings with apt
(gir1.2-gtk-3.0) I tried to following error message when starting the Python
code:
gi._glib.GError: XXX.glade: required gtk+ version 3.10, current version is 3.4
How could I best solve this?
Do I need to downgrade Gtk from 3.4 to 3.1? And if yes how do I do that?
How do I find version 3.10 in the repositories?
Or is something wrongly configured? Maybe the Gtk version being fixed in the
glade file which I imported from a different system, whose Gtk version might
have been different (do not remember)?
Or any other suggestions?
I do not think source code is helpful in this matter, as the problem most likely is located outside of it, nonetheless some extract from it:
from gi.repository import Gtk, GObject, GLib
wTree = Gtk.Builder()
class Test_GUI:
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("XXX.glade")
self.window = self.builder.get_object("MainScreen")
self.window.show_all()
OK, the solution was to recompile the .glade builder file.
Somehow the version conflict was that the glade file was created on
a different system than it got used at.
Can you tell us wich program you try to use ? Can you Modify the source code to fit with your Gtk version ? Does it work ?
The upgrade from Gtk 3.4 to 3.10 is not possible (with aptitude or apt-get); Gtk 3.10 can't be found in the official repository of debian (correct me if i'm wrong), see this link
However you can install a later unstable version, see this link (It's not recommand too).
dialog = gtk.FileChooserDialog("Open..",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
I want to use the above code for file browsing- but when I give the following import
import pygtk
pygtk.require('2.0')
import gtk
I get Error: No module named 'pygtk'
Do I need to give pygtk as a module inside my application folder? Do say with steps. Thank you.
pygtk is the old and deprecated gtk Python api. Do not use it unless you have a legacy application. The correct way nowadays is to use GObject-introspection to use Gtk3 from Python. Which is also really awesome as it makes Python a first-class supported language, with no wrappers necessary.
from gi.repository import Gtk
dlg = Gtk.FileChooserDialog()
dlg.show()
You should look at this: http://rox.sourceforge.net/desktop/node/245.html
It shows some problems. Good luck on fixing it :).
Edit:
This has lots of info on pip install (windows): How do I install pip on Windows?
I'm building my first Python program :) However, I installed Python 3.2 instead of 2.7, as the newer version has TkInter included. Now I can't find a way to use PIL in it.
I have read this question but as a total newcomer it's not much help for me. I installed zlib and the libjpeg but couldn't get any further when it comes to building the pil-py3k. I have no idea what I am supposed to do in here:
# --------------------------------------------------------------------
# Library pointers.
#
# Use None to look for the libraries in well-known library locations.
# Use a string to specify a single directory, for both the library and
# the include files. Use a tuple to specify separate directories:
# (libpath, includepath). Examples:
#
# JPEG_ROOT = "/home/libraries/jpeg-6b"
# TIFF_ROOT = "/opt/tiff/lib", "/opt/tiff/include"
#
# If you have "lib" and "include" directories under a common parent,
# you can use the "libinclude" helper:
#
# TIFF_ROOT = libinclude("/opt/tiff")
FREETYPE_ROOT = None
JPEG_ROOT = None
TIFF_ROOT = None
ZLIB_ROOT = None
TCL_ROOT = None
Is there an easier way to enable PIL with PNG and JPEG support for Python 3?
One option is to download an older version of Python but then there won't be TkInter?
I wish you understand I'm a newcomer so please forgive me if I make any stupid questions. Python seems really cool! ;) Any help is appreciated.
Unofficial PIL for Python 3.2 is the answer
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Just found out that there is PIL fork Pillow: https://pypi.python.org/pypi/Pillow/2.1.0
As agf mentioned in his comment on your question, tkinter has been included with every version of Python for years. The important difference is a restructuring of the tkinter packages. The biggest of those changes is the base package name: In Python 3.x, the tkinter package is named tkinter, while in Python 2.x, the package is named Tkinter (note the capital "T"). You can see the Python 2.7.2 docs on tkinter for more information.
Unfortunately, I haven't used PIL with Python 3.x, so I can't be of any assistance there, but hopefully you can accomplish what you need in 2.7 instead.
See http://mail.python.org/pipermail/image-sig/2012-October/007080.html for an updated port that works on Python 2+3
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.