PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用,不过只支持到Python 2.7
Pillow:是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。目前最新版本是3.0.0
1、安装
在Debian/Ubuntu Linux下直接通过apt安装:
1
|
$ sudo apt
-
get install python
-
imaging
|
Mac和其他版本的Linux可以直接使用easy_install或pip安装,安装前需要把编译环境装好:
1
|
$ sudo easy_install PIL
|
Windows平台就去PIL官方网站下载exe安装包或pip
1
|
$ pip install pillow
|
2、实现屏幕截图
1
2
3
|
from
PIL
import
ImageGrab
im
=
ImageGrab.grab()
im.save(
"1.png"
)
#定义保存的路径和保存的图片格式
|
1、安装
windows下:
1
2
|
32
位: http:
/
/
sourceforge.net
/
projects
/
pyqt
/
files
/
PyQt4
/
PyQt
-
4.11
.
4
/
PyQt4
-
4.11
.
4
-
gpl
-
Py2.
7
-
Qt4.
8.7
-
x32.exe
64
位: http:
/
/
sourceforge.net
/
projects
/
pyqt
/
files
/
PyQt4
/
PyQt
-
4.11
.
4
/
PyQt4
-
4.11
.
4
-
gpl
-
Py2.
7
-
Qt4.
8.7
-
x64.exe
|
linux下:
1
|
https:
/
/
www.riverbankcomputing.com
/
software
/
pyqt
/
download
|
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
sys,time
import
os.path
from
PyQt4
import
QtGui, QtCore, QtWebKit
class
PageShotter(QtGui.QWidget):
def
__init__(
self
, url, parent
=
None
):
QtGui.QWidget.__init__(
self
, parent)
self
.url
=
url
def
shot(
self
):
webView
=
QtWebKit.QWebView(
self
)
webView.load(QtCore.QUrl(
self
.url))
self
.webPage
=
webView.page()
self
.connect(webView, QtCore.SIGNAL(
"loadFinished(bool)"
),
self
.savePage)
def
savePage(
self
, finished):
if
finished:
print
"开始截图!"
size
=
self
.webPage.mainFrame().contentsSize()
print
"页面宽:%d,页面高:%d"
%
(size.width(), size.height())
self
.webPage.setViewportSize(QtCore.QSize(size.width()
+
16
, size.height()))
img
=
QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
painter
=
QtGui.QPainter(img)
self
.webPage.mainFrame().render(painter)
painter.end()
fileName
=
"shot.png"
if
img.save(fileName):
filePath
=
os.path.join(os.path.dirname(__file__), fileName)
print
"截图完毕:%s"
%
filePath
else
:
print
"截图失败"
else
:
print
"网页加载失败!"
self
.close()
if
__name__
=
=
"__main__"
:
app
=
QtGui.QApplication(sys.argv)
shotter
=
PageShotter(
"https://www.jd.com/"
)
shotter.shot()
sys.exit(app.exec_())