WebBrowser/Tools/WebBrowserTools.py

branch
QtWebEngine
changeset 4865
4adc526bc4b3
parent 4810
f68d0446609e
child 5389
9b1c800daff3
--- a/WebBrowser/Tools/WebBrowserTools.py	Wed Mar 16 19:55:09 2016 +0100
+++ b/WebBrowser/Tools/WebBrowserTools.py	Thu Mar 17 20:22:20 2016 +0100
@@ -16,7 +16,8 @@
 import os
 import re
 
-from PyQt5.QtCore import QFile, QByteArray, QUrl, QCoreApplication
+from PyQt5.QtCore import QFile, QByteArray, QUrl, QCoreApplication, QBuffer, \
+    QIODevice
 from PyQt5.QtGui import QPixmap
 
 
@@ -152,6 +153,41 @@
     return pixmap
 
 
+def pixmapToByteArray(pixmap):
+    """
+    Module function to convert a pixmap to a byte array containing the pixmap
+    as a PNG encoded as base64.
+    
+    @param pixmap pixmap to be converted
+    @type QPixmap
+    @return byte array containing the pixmap
+    @rtype QByteArray
+    """
+    byteArray = QByteArray()
+    buffer = QBuffer(byteArray)
+    buffer.open(QIODevice.WriteOnly)
+    if pixmap.save(buffer, "PNG"):
+        return buffer.buffer().toBase64()
+    
+    return QByteArray()
+
+
+def pixmapToDataUrl(pixmap):
+    """
+    Module function to convert a pixmap to a data: URL.
+    
+    @param pixmap pixmap to be converted
+    @type QPixmap
+    @return data: URL
+    @rtype QUrl
+    """
+    data = bytes(pixmapToByteArray(pixmap)).decode()
+    if data:
+        return QUrl("data:image/png;base64," + data)
+    else:
+        return QUrl()
+
+
 def getWebEngineVersions():
     """
     Module function to extract the web engine version from the default user

eric ide

mercurial