Python 3 的图像库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3896286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image library for Python 3
提问by banx
What is python-3 using instead of PIL for manipulating Images?
python-3 使用什么代替 PIL 来操作图像?
回答by s0mebody
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.
截至 2012 年 3 月 30 日,我已尝试在 GitHub 上获取 sloonz fork 以打开图像,但未能成功。我让它编译正常,但它实际上并没有工作。我也尝试构建 gohlke 的库,它也编译但无法打开任何图像。上面有人提到了 PythonMagick,但它只能在 Windows 上编译。请参阅wxPython wiki 上的 PythonMagick。
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.
PIL 上一次更新是在 2009 年,虽然它的网站说他们正在开发 Python 3 端口,但已经 3 年了,邮件列表已经变冷了。
To solve my Python 3 image manipulation problem, I am using subprocess.call()to execute ImageMagick shell commands. This method works.
为了解决我的 Python 3 图像处理问题,我使用subprocess.call()执行 ImageMagick shell 命令。这种方法有效。
See the subprocess module documentation.
请参阅子流程模块文档。
回答by xob
Christoph Gohlke managed to build PIL (for Windows only) for python versions up to 3.3: http://www.lfd.uci.edu/~gohlke/pythonlibs/
Christoph Gohlke 设法为高达 3.3 的 Python 版本构建了 PIL(仅适用于 Windows):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.
我用 Python 3.2 尝试了他的 PIL 版本,并打开/创建/像素操作/保存所有工作。
回答by luispedro
回答by Oleh Prypin
Qt works very well with graphics. In my opinion it is more versatile than PIL.
Qt 与图形工作得很好。在我看来,它比 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.
您可以获得图形处理所需的所有功能,但也有矢量图形,甚至支持真实打印机。所有这些都在一个统一的 API 中,QPainter.
To use Qt you need a Python binding for it: PySideor PyQt4.
They both support Python 3.
要使用 Qt,您需要一个 Python 绑定:PySide或PyQt4。
它们都支持 Python 3。
Here is a simple example that loads a JPG image, draws an antialiased circle of radius 10at coordinates (20, 20)with the color of the pixel that was at those coordinates and saves the modified image as a PNG file:
这是一个简单的示例,它加载 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')
But please note that this solution is quite 'heavyweight', because Qt is a large framework for making GUI applications.
但请注意,这个解决方案相当“重量级”,因为 Qt 是一个用于制作 GUI 应用程序的大型框架。
回答by Janus Troelsen
The "friendly PIL fork" Pillowworks on Python 2 and 3. Check out the Github projectfor support matrix and so on.
回答by speedplane
回答by linhares
Depending on what is needed, scikit-imagemay 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:
根据需要,scikit-image可能是最佳选择,其操作远远超出 PIL 和当前版本的 Pillow。维护得很好,至少和枕头一样好。此外,底层数据结构来自 Numpy 和 Scipy,这使得其代码具有难以置信的互操作性。枕头不能处理的例子:
You can see its power in the gallery. This paperprovides a great intro to it. Good luck!
回答by bunkus
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/PILasOPENCVor install it with pip install PILasOPENCV. Not all PIL functions have been simulated but the most common functions work.
如果您使用的是 Python3,您还可以使用在 Python 2 和 3 中工作的库 PILasOPENCV。函数 api 调用与 PIL 或枕头中的相同,但在内部它与 OpenCV 和 numpy 一起工作以加载、保存和操作图像。查看https://github.com/bunkahle/PILasOPENCV或使用 pip install PILasOPENCV 安装它。并非所有 PIL 函数都已被模拟,但最常见的函数可以工作。


