本文整理汇总了Python中PyQt4.QtCore.QBuffer类的典型用法代码示例。如果您正苦于以下问题:Python QBuffer类的具体用法?Python QBuffer怎么用?Python QBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: screenshot
def screenshot(self):
logging.debug('screenshot() invoked')
frame = self.page().mainFrame()
size = frame.contentsSize()
if (self.screenWidth):
size.setWidth(self.screenWidth)
if (self.screenHeight):
size.setHeight(self.screenHeight)
if (self.minWidth and self.minWidth > self.screenWidth):
size.setWidth(self.minWidth)
if (self.minHeight and self.minHeight > self.screenHeight):
size.setHeight(self.minHeight)
self.page().setViewportSize(size)
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
image.save(buffer, self.format, self.quality)
logging.debug('screenshot() returned image of type ' + self.content_type + ' of length ' + str(buffer.data().length()))
return { 'content': buffer.data(), 'content_type': self.content_type }
示例2: request
def request(self, iri, method="GET", extra_headers=None,
oauth_credentials=None, post_content=None):
"""Return a deferred that will be fired with a Response object."""
uri = self.iri_to_uri(iri)
request = QNetworkRequest(QUrl(uri))
request.setSslConfiguration(self.ssl_config)
headers = yield self.build_request_headers(uri, method, extra_headers,
oauth_credentials)
for key, value in headers.items():
request.setRawHeader(key, value)
post_buffer = QBuffer()
post_buffer.setData(post_content)
try:
result = yield self._perform_request(request, method, post_buffer)
except ProxyUnauthorizedError as e:
app_proxy = QNetworkProxy.applicationProxy()
proxy_host = app_proxy.hostName() if app_proxy else "proxy server"
got_creds = yield self.request_proxy_auth_credentials(
proxy_host, self.proxy_retry)
if got_creds:
self.proxy_retry = True
result = yield self.request(iri, method, extra_headers,
oauth_credentials, post_content)
else:
excp = WebClientError('Proxy creds needed.', e)
defer.returnValue(excp)
defer.returnValue(result)
示例3: render_to_bytes
def render_to_bytes(self, url):
"""Renders the image into an object of type 'str'"""
format = self.format # this may not be constant due to processEvents()
image = self.render(url)
qBuffer = QBuffer()
image.save(qBuffer, format)
return qBuffer.buffer().data()
示例4: from_QImage
def from_QImage(self, img):
buf = QByteArray()
bf = QBuffer(buf)
bf.open(QIODevice.WriteOnly)
img.save(bf, format='PNG')
self.buf = buf.data()
self.format = PackedQImage.FORMAT_PNG
self._encode()
示例5: newfunc
def newfunc():
ba = QByteArray(data) # keep reference to avoid GC
f = QBuffer(ba)
f.open(mode)
try:
func(f)
finally:
f.close()
示例6: render_to_file
def render_to_file(self, url, file_object):
"""Renders the image into a File resource.
Returns the size of the data that has been written.
"""
format = self.format # this may not be constant due to processEvents()
image = self.render(url)
qBuffer = QBuffer()
image.save(qBuffer, format, quality=self.quality)
file_object.write(qBuffer.buffer().data())
return qBuffer.size()
示例7: get_html_img
def get_html_img(scene):
"""
Create HTML img element with base64-encoded image from the scene
"""
byte_array = QByteArray()
filename = QBuffer(byte_array)
filename.open(QIODevice.WriteOnly)
PngFormat.write(filename, scene)
img_encoded = byte_array.toBase64().data().decode("utf-8")
return "<img src='data:image/png;base64,%s'/>" % img_encoded
示例8: pixmap_to_imgformat
def pixmap_to_imgformat(pixmap, format):
import io
from PyQt4.QtCore import QBuffer, QByteArray, QIODevice
byte_array = QByteArray()
buffer = QBuffer(byte_array)
buffer.open(QIODevice.WriteOnly)
pixmap.save(buffer, format)
string_io = io.BytesIO(byte_array)
string_io.seek(0)
return string_io.read()
示例9: getImage
def getImage(self):
""" Return the loaded image """
if self.isDefault:
return None
pix = self.orignalimage
by = QByteArray()
buf = QBuffer(by)
buf.open(QIODevice.WriteOnly)
pix.save(buf, "JPG")
return by
示例10: _getPng
def _getPng(self, width=None, height=None):
image = QImage(self.web_page.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
self.web_page.mainFrame().render(painter)
painter.end()
if width:
image = image.scaledToWidth(width, Qt.SmoothTransformation)
if height:
image = image.copy(0, 0, width, height)
b = QBuffer()
image.save(b, "png")
return bytes(b.data())
示例11: saveTiles
def saveTiles(self):
if self.tiles is None:
QMessageBox.warning(None, self.plugin.pluginName, self.tr("No tiles have been downloaded."))
return
# Let the user choose the directory to save to
directory = QFileDialog.getExistingDirectory(caption=self.tr("{}: Choose directory").format(self.layerDef.title))
if not directory:
# User cancelled the directory selection
return
# Build the content of the .aux.xml file containing the projection info
projection_string = (self.crs().toWkt())
pam_string = '<PAMDataset>\n' + \
'<SRS>{}</SRS>\n'.format(projection_string) + \
'</PAMDataset>'
for tile in self.tiles.tiles.values():
# Figure out the file format extension
reader = QImageReader()
buffer = QBuffer()
buffer.setData(tile.data)
buffer.open(QIODevice.ReadOnly)
extension = str(reader.imageFormat(buffer))
# Build the file name of the image file
image_file_name = "{}-{}-{}.{}".format(tile.x, tile.y, tile.zoom, extension)
image_file_path = join(directory, image_file_name)
# Save the image file
with open(image_file_path, 'wb') as image_file:
image_file.write(tile.data)
# Save the .aux.xml
with open(image_file_path + '.aux.xml', 'w') as aux_file:
aux_file.write(pam_string)
# Save the world file containing the georeferencing information
tile_rect = self.tiles.serviceInfo.getTileRect(tile.zoom, tile.x, tile.y)
tile_size = self.tiles.TILE_SIZE
with open(image_file_path + 'w', 'w') as world_file:
world_file.writelines([
str(tile_rect.width() / tile_size) + '\n',
'0\n',
'0\n',
str(-tile_rect.height() / tile_size) + '\n',
str(tile_rect.xMinimum()) + '\n',
str(tile_rect.yMaximum()) + '\n'
])
# Done
msg = self.tr("Tiles have been saved.")
self.showMessageBar(msg, QgsMessageBar.INFO, 2)
示例12: render_layers
def render_layers(layer_paths):
"""
:param layer_paths: A list of layer paths.
:return: Buffer containing output. Note caller is responsible for closing
the buffer with buffer.close()
:rtype: QBuffer
"""
layers = []
extent = None
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(3857)
for layer_path in layer_paths:
map_layer = QgsVectorLayer(layer_path, None, 'ogr')
QgsMapLayerRegistry.instance().addMapLayer(map_layer)
transform = QgsCoordinateTransform(map_layer.crs(), crs)
print map_layer.extent().toString()
layer_extent = transform.transform(map_layer.extent())
if extent is None:
extent = layer_extent
else:
extent.combineExtentWith(layer_extent)
print extent.toString()
# set layer set
layers.append(map_layer.id()) # add ID of every layer
map_settings = QgsMapSettings()
map_settings.setDestinationCrs(crs)
map_settings.setCrsTransformEnabled(True)
map_settings.setExtent(extent)
map_settings.setOutputSize(QSize(1000, 1000))
map_settings.setLayers(layers)
# job = QgsMapRendererParallelJob(settings)
job = QgsMapRendererSequentialJob(map_settings)
job.start()
job.waitForFinished()
image = job.renderedImage()
# Save teh image to a buffer
map_buffer = QBuffer()
map_buffer.open(QIODevice.ReadWrite)
image.save(map_buffer, "PNG")
image.save('/tmp/test.png', 'png')
# clean up
QgsMapLayerRegistry.instance().removeAllMapLayers()
return map_buffer
示例13: _render
def _render(self):
self.setViewportSize(QSize(self.vwidth, self.vheight))
image = QImage(self.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
self.mainFrame().render(painter)
painter.end()
if self.width:
image = image.scaledToWidth(self.width, Qt.SmoothTransformation)
if self.height:
image = image.copy(0, 0, self.width, self.height)
b = QBuffer()
image.save(b, "png")
return str(b.data())
示例14: grab_svg
def grab_svg(scene):
"""
Return a SVG rendering of the scene contents.
Parameters
----------
scene : :class:`CanvasScene`
"""
from PyQt4.QtSvg import QSvgGenerator
svg_buffer = QBuffer()
gen = QSvgGenerator()
gen.setOutputDevice(svg_buffer)
items_rect = scene.itemsBoundingRect().adjusted(-10, -10, 10, 10)
if items_rect.isNull():
items_rect = QRectF(0, 0, 10, 10)
width, height = items_rect.width(), items_rect.height()
rect_ratio = float(width) / height
# Keep a fixed aspect ratio.
aspect_ratio = 1.618
if rect_ratio > aspect_ratio:
height = int(height * rect_ratio / aspect_ratio)
else:
width = int(width * aspect_ratio / rect_ratio)
target_rect = QRectF(0, 0, width, height)
source_rect = QRectF(0, 0, width, height)
source_rect.moveCenter(items_rect.center())
gen.setSize(target_rect.size().toSize())
gen.setViewBox(target_rect)
painter = QPainter(gen)
# Draw background.
painter.setBrush(QBrush(Qt.white))
painter.drawRect(target_rect)
# Render the scene
scene.render(painter, target_rect, source_rect)
painter.end()
buffer_str = str(svg_buffer.buffer())
return unicode(buffer_str.decode("utf-8"))
示例15: capture_image
def capture_image(session, selector=None, outformat="jpg"):
""" capture page as image """
# do capture now
cap = None
if selector is not None:
cap = session.capture(selector=selector)
else:
cap = session.capture()
# create a buffered image
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
cap.save(buffer, 'JPG' if outformat.upper() in ('JPG', 'JPEG') else 'PNG')
buffer.close()
return buffer, 'image/' + 'jpeg' if outformat.upper() in ('JPG','JPEG') else 'png'