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
Related
I was GOING to ask for help with this annoying problem, but I have solved it and want to document the stupid solution for the next person who searches SO for the string __cg_jpeg_resync_to_restart and gets (literally) 47 hits.
Here's the problem: on attempting to use tkinter for the first time with Python3, the following error may occur when you import tkinter:
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/Current/lib/python3.8/lib-dynload/_tkinter.cpython-38-darwin.so, 2): Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Python doc is clear that 3.7+ contain their own copy of tkinter, and in fact the first line above confirms that. So the issue isn't a wrong version of tkinter or some other tkinter install issue. But tkinter is trying to link to the MacOS copy of ImagIO, and that is trying to dynamically link what should be a perfectly standard thing, libJPEG.dylib. However that lib apparently doesn't define an entry point __cg_jpeg_resync_to_restart that ImagIO expects.
Now what?
This sounds like a version mismatch, ImageIO is getting a JPEG lib of a different version than it expects. But why? I check using homebrew, I have the very latest jpeg (9), it is in /usr/local/lib/libjpeg.dylib. That's odd, the name is all lowercase? I tell homebrew to uninstall its version of the jpeg cask, and it will not do that because libjpeg is a prereq of ghostscript, imagemagick, and about 6 other things brew has installed.
Most of the SO hits on __cg_jpeg_resync_to_restart talk about re-aiming DYLD_LIBRARY_PATH various ways and in fact that is the solution. After over 3 hours of futzing around, trying to find other versions of libjpeg, I finally look in the one place that is called out in the message(!),
$ find /System/Library/Frameworks/ImageIO.framework -name '*.dylib'
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJP2.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libOpenEXR.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib
Bingo. Look, it even uses capital letter JPEG in the name, as in the message! The ImageIO distributed with MacOS has its own private collection of libs, and when I set DYLD_LIBRARY_PATH for Python's use, I confused it. Adding /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources at the head of DYLD_LIBRARY_PATH before starting Python3 and tkinter imports fine!
It remains to be seen if other modules that expect libjpeg.dylib version 9 are going to barf now. Or will they ignore libJPEG and go on to /usr/local/libjpeg? I would appreciate comments on that and any issues here.
Is there an exif library out there for Python 3.x? It seems every exif library I run into is for Python 2.x only. I don't need anything too fancy. Just reading the values is enough.
Option 1. Use pyexiv2. See: pyexiv2 Bug #824440: Python 3 support You need boost-python for py3k and also to manually apply the patch posted at the end of the bug above, but aside from that it works. Probably easiest to get up and running under latest Ubuntu.
Option 2. Use PIL Downside: this branch/fork doesn't seem to be actively developed.
from PIL import Image
from PIL.ExifTags import TAGS
image = Image.open("test.jpg")
exif = image._getexif()
# decode exif using TAGS
Option 3. Use PythonMagick
from PythonMagick import Image
img = Image("image.jpg")
print img.attribute("EXIF:Orientation")
See also: Exif manipulation library for python
For reference, the pyexiv2 homepage now has a deprecation warning which points to Gexiv2, a GObject-introspection based wrapper around libexiv2 (the same library pyexiv2 wraps) specifically for the purpose of Python 3.x support.
Unfortunately, at the time of writing, installation of Gexiv2 is still painful and thus far I've been unable to get it working on Ubuntu Precise (looks like the libs are out of date - probably serves me right for sticking around on an LTS...), so PIL is still the best option for reading EXIF tags in Python 3.
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.
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.
Where can i get OpenCV for python?What are the pre-requisites?? i tried to install opencv-python through synaptic package manager but Python says
No module named CVtypes
CVTypes is a third party implementation that essentially wraps python around objects written in C, the language that OpenCV is written in (along with C++). If you want to use that, you will have to download and install it separately, as it is not part of the standard repositories of Ubuntu's Synaptic package manager that I know of at this time (I assume you are on Ubuntu because you mentioned 'Synaptic', Ubuntu's package manager).
However, there is an official python interface for OpenCV that is included in the OpenCV SVN repository and build packages. When installing version 1.0 from the package manager in Ubuntu, the python modules will be installed in the following directory:
/usr/lib/pymodules/python2.6/opencv
Ensure that is part of your PYTHONPATH environment variable and you should be able to import the modules as such:
from opencv.cv import *
from opencv.highgui import *
OpenCV over time has accumulated numerous Python bindings, mostly due to the strange way arrays are represented in OpenCV (IMHO). Here is a short list:
PyOpenCV
Scikits Image
Ctypes OpenCV
SWIG OpenCV
Choose which one you want to use and keep it consistent and upto date. I personally prefer the classic WillowGarage version[listed last] over its fancier cousins since it has most development and test muscle behind it.
get it from here unofficial binary packages.
by the way, they provide unofficial packages for many other projects
Tried the official website? http://opencv.willowgarage.com/wiki/Welcome
check your openCV version. Version 2 needs a simple
import cv
you may have a look at the samples/python folder.
this webpage explains in great depth on the installation
http://opencvpython.blogspot.com/2012/05/install-opencv-in-windows-for-python.html
after the installation try out the samples provided by opencv\samples\python2