Skip to content

Commit

Permalink
Allow render() to save gif files. Requires PIL.
Browse files Browse the repository at this point in the history
old-commit-id: c513f6430f8abdd9c0e29428bb6ca7f671e294b8
  • Loading branch information
IceArmy committed Sep 27, 2011
1 parent 15e0399 commit c3c56cf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
10 changes: 7 additions & 3 deletions INSTALL.md
Expand Up @@ -5,7 +5,8 @@
* PyQt4 >= 4.8.0
* Qt >= 4.7.0

* python-argparse == (Python 2.6)
* python-argparse == (Python 2.6 only)
* PIL (python-imaging-library) >= 1.1.7 (optional; for rendering to GIF)

### INSTALLING
-------------------------
Expand All @@ -16,11 +17,14 @@ Windows
>
[Python](http://www.python.org/download/)
[PyQt4](http://www.riverbankcomputing.co.uk/software/pyqt/download)
Qt4 - PyQt4 comes packaged with the Qt runtime library(s)
> Qt4 - PyQt4 comes packaged with the Qt runtime library(s)
>
[python-argparse](http://pypi.python.org/pypi/argparse/)
[PIL](http://www.pythonware.com/products/pil/)

Ubuntu

> Open a terminal window, and enter the command `sudo apt-get install python-qt4`
> Open a terminal window, and enter the command `sudo apt-get install python-qt4 python-imaging`
>
All the required packages should be automatically pulled in and installed.

Expand Down
41 changes: 38 additions & 3 deletions pyphantomjs/webpage.py
Expand Up @@ -17,18 +17,25 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

from cStringIO import StringIO
from math import ceil, floor

import sip
from PyQt4.QtCore import (pyqtProperty, pyqtSignal, pyqtSlot, QByteArray,
QDir, QEvent, QEventLoop, QFileInfo, QObject,
QPoint, QRect, QSize, QSizeF, Qt, QUrl)
from PyQt4.QtCore import (pyqtProperty, pyqtSignal, pyqtSlot, QBuffer,
QByteArray, QDir, QEvent, QEventLoop, QFileInfo,
QObject, QPoint, QRect, QSize, QSizeF, Qt, QUrl,
qDebug)
from PyQt4.QtGui import (QApplication, QDesktopServices, QImage,
QMouseEvent, QPainter, QPalette, QPrinter,
QRegion, qRgba)
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt4.QtWebKit import QWebPage, QWebSettings

try:
from PIL import Image
except ImportError:
qDebug('PIL not found! Saving to gif files will be disabled.')

from networkaccessmanager import NetworkAccessManager
from plugincontroller import do_action
from utils import injectJsInFrame
Expand Down Expand Up @@ -147,6 +154,32 @@ def finish(self, ok):
def mainFrame(self):
return self.m_mainFrame

def renderGif(self, image, fileName):
try:
Image
except NameError:
return False

buffer_ = QBuffer()
buffer_.open(QBuffer.ReadWrite)
image.save(buffer_, 'PNG')

stream = StringIO()
stream.write(buffer_.data())
buffer_.close()
stream.seek(0)
pilimg = Image.open(stream)

# use the adaptive quantizer instead of the web quantizer; eases off of grainy images
pilimg = pilimg.convert('RGB').convert('P', palette=Image.ADAPTIVE)

try:
pilimg.save(fileName)
return True
except IOError as (t, e):
qDebug("WebPage.renderGif - %s: '%s'" % (e, fileName))
return False

def renderImage(self):
contentsSize = self.m_mainFrame.contentsSize()
contentsSize -= QSize(self.m_scrollPosition.x(), self.m_scrollPosition.y())
Expand Down Expand Up @@ -409,6 +442,8 @@ def render(self, fileName):
return self.renderPdf(fileName)

image = self.renderImage()
if fileName.lower().endswith('.gif'):
return self.renderGif(image, fileName)

return image.save(fileName)

Expand Down
16 changes: 11 additions & 5 deletions setup.py
Expand Up @@ -36,11 +36,17 @@ def read(fname):
else:
README += INSTALL

try:
import PyQt4
except ImportError:
print ('\nWARNING: PyPhantomJS requires PyQt4, which was not found!\n'
' Refer to the README or INSTALL file for installation information.\n')
mainWarningPrinted = False
mainWarning = ('\nWARNING: PyPhantomJS requires additional libraries, which were not found!\n'
' Refer to the README or INSTALL file for installation information.\n')
for module in ('PyQt4', 'PIL'):
try:
__import__(module)
except ImportError:
if not mainWarningPrinted:
print mainWarning
mainWarningPrinted = True
print "* '%s' library not found" % module


setup(
Expand Down

0 comments on commit c3c56cf

Please sign in to comment.