问题:适用于Python 3的图片库

什么是python-3而不是PIL来处理图像?

What is python-3 using instead of PIL for manipulating Images?


回答 0

“友好的PIL叉子” 枕头可在Python 2和3上使用。请查看Github项目以获取支持矩阵等。

The “friendly PIL fork” Pillow works on Python 2 and 3. Check out the Github project for support matrix and so on.


回答 1

Christoph Gohlke设法为高达3.3的python版本构建了PIL(仅适用于Windows):http ://www.lfd.uci.edu/~gohlke/pythonlibs/

我用Python 3.2尝试了他的PIL版本,并且图像打开/创建/像素操作/保存了所有工作。

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.


回答 2

Qt与图形效果很好。在我看来,它比PIL更通用。

您可以获得图形处理所需的所有功能,但也有矢量图形,甚至支持实际打印机。而所有这些都在一个统一的API中QPainter

要使用Qt,您需要一个Python绑定:PySidePyQt4
它们都支持Python 3。

这是一个简单的示例,该示例加载JPG图像,在坐标(20,20)处绘制半径为10的抗锯齿圆,并使用这些坐标处的像素颜色,并将修改后的图像另存为PNG文件:

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')

但是请注意,此解决方案是“重量级”的,因为Qt是用于制作GUI应用程序的大型框架。

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.


回答 3

截至2012年3月30日,我尝试并未能在GitHub上使用sloonz fork打开图像。我把它编译好了,但是实际上没有用。我还尝试构建gohlke的库,它也进行了编译,但是无法打开任何图像。有人在上面提到过PythonMagick,但它只能在Windows上编译。请参阅wxPython Wiki上的PythonMagick

PIL的最新更新是在2009年,虽然它的网站说他们正在使用Python 3端口,但是已经过去了3年,并且邮件列表变得越来越冷。

为了解决我的Python 3图像处理问题,我正在使用subprocess.call()执行ImageMagick shell命令。此方法有效。

请参阅子流程模块文档

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.


回答 4

您可以在Python 3上使用我的软件包mahotas。它是基于numpy的,而不是基于PIL的。

You can use my package mahotas on Python 3. It is numpy-based rather than PIL based.


回答 5

您需要Pillow,这是在Python 3上安装它的方法:

pip3 install Pillow

如果那对您不起作用(应该),请尝试normal pip

pip install Pillow

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

回答 6

根据需要,scikit-image可能是最佳选择,其处理方式已经超越了PIL和当前版本的Pillow。保养得很好,至少和枕头一样多。而且,底层的数据结构来自Numpy和Scipy,这使其代码具有令人难以置信的互操作性。枕头无法处理的示例:

区域邻接图合并

on变换

定向梯度直方图

近似和细分多边形

您可以在图库中看到其力量。 本文提供了一个很好的介绍。祝好运!

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:

Region Adjacency Graph Merging

Radon Transform

Histogram of oriented gradients

Approximate and subdivide polygons

You can see its power in the gallery. This paper provides a great intro to it. Good luck!


回答 7

如果您使用的是Python3,还可以使用库PILasOPENCV,该库可在Python 2和3中使用。函数api调用与PIL或pillow中的函数相同,但在内部它与OpenCV和numpy一起加载,保存和操作图像。看看https://github.com/bunkahle/PILasOPENCV或使用pip install PILasOPENCV进行安装。并非所有PIL功能都已被模拟,但是最常用的功能都可以工作。

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。