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
Related
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.
I'm having trouble in finding PIL source code.
The main page of the library http://www.pythonware.com/products/pil/ does not have any link to git repositories.
Is Pillow (https://github.com/python-pillow/Pillow) repository correct project?
PIL was never ported to Python 3, so Pillow forked the project and took it over. Pillow has since been back-ported to Python 2, but if you are working with Python 3, you must use Pillow. They are essentially the same.
If you want the source code of PIL, just download it and look within the files yourself. If you want the documentation for PIL, this is a good reference.
No. I think it is difference project.
https://pillow.readthedocs.io/en/latest/installation.html
When you want to install pillow, you must uninstall PIL
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 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.