本文整理汇总了Python中qtpy.QtGui.QPixmap.loadFromData方法的典型用法代码示例。如果您正苦于以下问题:Python QPixmap.loadFromData方法的具体用法?Python QPixmap.loadFromData怎么用?Python QPixmap.loadFromData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qtpy.QtGui.QPixmap
的用法示例。
在下文中一共展示了QPixmap.loadFromData方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_zoom_figure_viewer
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def test_zoom_figure_viewer(figbrowser, tmpdir, fmt):
"""
Test zooming in and out the figure diplayed in the figure viewer.
"""
fig = add_figures_to_browser(figbrowser, 1, tmpdir, fmt)[0]
figcanvas = figbrowser.figviewer.figcanvas
# Calculate original figure size in pixels.
qpix = QPixmap()
qpix.loadFromData(fig, fmt.upper())
fwidth, fheight = qpix.width(), qpix.height()
assert figbrowser.zoom_disp.value() == 100
assert figcanvas.width() == fwidth
assert figcanvas.height() == fheight
# Zoom in and out the figure in the figure viewer.
scaling_factor = 0
scaling_step = figbrowser.figviewer._scalestep
for zoom_step in [1, 1, -1, -1, -1]:
if zoom_step == 1:
figbrowser.zoom_in()
elif zoom_step == -1:
figbrowser.zoom_out()
scaling_factor += zoom_step
scale = scaling_step**scaling_factor
assert figbrowser.zoom_disp.value() == np.floor(scale * 100)
assert figcanvas.width() == np.floor(fwidth * scale)
assert figcanvas.height() == np.floor(fheight * scale)
示例2: test_save_all_figures
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def test_save_all_figures(figbrowser, tmpdir, mocker, fmt):
"""
Test saving all figures contained in the thumbnail scrollbar in batch
into a single directory.
"""
figs = add_figures_to_browser(figbrowser, 3, tmpdir, fmt)
# Save all figures, but cancel the dialog to get a directory.
mocker.patch(
'spyder.plugins.plots.widgets.figurebrowser.getexistingdirectory',
return_value=None)
fignames = figbrowser.save_all_figures()
assert fignames is None
# Save all figures.
mocker.patch(
'spyder.plugins.plots.widgets.figurebrowser.getexistingdirectory',
return_value=to_text_string(tmpdir.mkdir('all_saved_figures')))
fignames = figbrowser.save_all_figures()
assert len(fignames) == len(figs)
for fig, figname in zip(figs, fignames):
expected_qpix = QPixmap()
expected_qpix.loadFromData(fig, fmt.upper())
saved_qpix = QPixmap()
saved_qpix.load(figname)
assert osp.exists(figname)
assert expected_qpix.toImage() == saved_qpix.toImage()
示例3: copy_figure
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def copy_figure(self):
"""Copy figure to clipboard."""
if self.fmt in ['image/png', 'image/jpeg']:
qpixmap = QPixmap()
qpixmap.loadFromData(self.fig, self.fmt.upper())
QApplication.clipboard().setImage(qpixmap.toImage())
elif self.fmt == 'image/svg+xml':
svg_to_clipboard(self.fig)
else:
return
self.blink_figure()
示例4: test_save_figure_to_file
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def test_save_figure_to_file(figbrowser, tmpdir, mocker, fmt, fext):
"""
Test saving png and svg figures to file with the figure browser.
"""
fig = add_figures_to_browser(figbrowser, 1, tmpdir, fmt)[0]
expected_qpix = QPixmap()
expected_qpix.loadFromData(fig, fmt.upper())
# Save the figure to disk with the figure browser.
saved_figname = osp.join(to_text_string(tmpdir), 'spyfig' + fext)
mocker.patch('spyder.plugins.plots.widgets.figurebrowser.getsavefilename',
return_value=(saved_figname, fext))
figbrowser.save_figure()
saved_qpix = QPixmap()
saved_qpix.load(saved_figname)
assert osp.exists(saved_figname)
assert expected_qpix.toImage() == saved_qpix.toImage()
示例5: test_save_thumbnails
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def test_save_thumbnails(figbrowser, tmpdir, qtbot, mocker, fmt):
"""
Test saving figures by clicking on the thumbnail icon.
"""
figs = add_figures_to_browser(figbrowser, 3, tmpdir, fmt)
fext = '.svg' if fmt == 'image/svg+xml' else '.png'
# Save the second thumbnail of the scrollbar.
figname = osp.join(to_text_string(tmpdir), 'figname' + fext)
mocker.patch('spyder.plugins.plots.widgets.figurebrowser.getsavefilename',
return_value=(figname, fext))
qtbot.mouseClick(
figbrowser.thumbnails_sb._thumbnails[1].savefig_btn, Qt.LeftButton)
expected_qpix = QPixmap()
expected_qpix.loadFromData(figs[1], fmt.upper())
saved_qpix = QPixmap()
saved_qpix.load(figname)
assert osp.exists(figname)
assert expected_qpix.toImage() == saved_qpix.toImage()
示例6: FigureCanvas
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
class FigureCanvas(QFrame):
"""
A basic widget on which can be painted a custom png, jpg, or svg image.
"""
def __init__(self, parent=None, background_color=None):
super(FigureCanvas, self).__init__(parent)
self.setLineWidth(2)
self.setMidLineWidth(1)
self.setObjectName("figcanvas")
self.setStyleSheet(
"#figcanvas {background-color:" + str(background_color) + "}")
self.fig = None
self.fmt = None
self.fwidth, self.fheight = 200, 200
self._blink_flag = False
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.context_menu_requested)
def context_menu_requested(self, event):
"""Popup context menu."""
if self.fig:
pos = QPoint(event.x(), event.y())
context_menu = QMenu(self)
context_menu.addAction(ima.icon('editcopy'), "Copy Image",
self.copy_figure,
QKeySequence(
get_shortcut('plots', 'copy')))
context_menu.popup(self.mapToGlobal(pos))
@Slot()
def copy_figure(self):
"""Copy figure to clipboard."""
if self.fmt in ['image/png', 'image/jpeg']:
qpixmap = QPixmap()
qpixmap.loadFromData(self.fig, self.fmt.upper())
QApplication.clipboard().setImage(qpixmap.toImage())
elif self.fmt == 'image/svg+xml':
svg_to_clipboard(self.fig)
else:
return
self.blink_figure()
def blink_figure(self):
"""Blink figure once."""
if self.fig:
self._blink_flag = not self._blink_flag
self.repaint()
if self._blink_flag:
timer = QTimer()
timer.singleShot(40, self.blink_figure)
def clear_canvas(self):
"""Clear the figure that was painted on the widget."""
self.fig = None
self.fmt = None
self._qpix_buffer = []
self.repaint()
def load_figure(self, fig, fmt):
"""
Load the figure from a png, jpg, or svg image, convert it in
a QPixmap, and force a repaint of the widget.
"""
self.fig = fig
self.fmt = fmt
if fmt in ['image/png', 'image/jpeg']:
self._qpix_orig = QPixmap()
self._qpix_orig.loadFromData(fig, fmt.upper())
elif fmt == 'image/svg+xml':
self._qpix_orig = QPixmap(svg_to_image(fig))
self._qpix_buffer = [self._qpix_orig]
self.fwidth = self._qpix_orig.width()
self.fheight = self._qpix_orig.height()
def paintEvent(self, event):
"""Qt method override to paint a custom image on the Widget."""
super(FigureCanvas, self).paintEvent(event)
# Prepare the rect on which the image is going to be painted :
fw = self.frameWidth()
rect = QRect(0 + fw, 0 + fw,
self.size().width() - 2 * fw,
self.size().height() - 2 * fw)
if self.fig is None or self._blink_flag:
return
# Check/update the qpixmap buffer :
qpix2paint = None
for qpix in self._qpix_buffer:
if qpix.size().width() == rect.width():
qpix2paint = qpix
break
else:
if self.fmt in ['image/png', 'image/jpeg']:
#.........这里部分代码省略.........
示例7: handleTileDataLoaded
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def handleTileDataLoaded(self, x, y, zoom, data):
pix = QPixmap()
pix.loadFromData(data)
self.tileReceived.emit(x, y, zoom, pix)
示例8: png_to_qimage
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
def png_to_qimage(png):
"""Return a QImage from the raw data of a png image."""
qpix = QPixmap()
qpix.loadFromData(png, 'image/png'.upper())
return qpix.toImage()
示例9: FigureCanvas
# 需要导入模块: from qtpy.QtGui import QPixmap [as 别名]
# 或者: from qtpy.QtGui.QPixmap import loadFromData [as 别名]
class FigureCanvas(QFrame):
"""
A basic widget on which can be painted a custom png, jpg, or svg image.
"""
def __init__(self, parent=None):
super(FigureCanvas, self).__init__(parent)
self.setLineWidth(2)
self.setMidLineWidth(1)
self.setStyleSheet("background-color: white")
self.fig = None
self.fmt = None
self.fwidth, self.fheight = 200, 200
def clear_canvas(self):
"""Clear the figure that was painted on the widget."""
self.fig = None
self.fmt = None
self._qpix_buffer = []
self.repaint()
def load_figure(self, fig, fmt):
"""
Load the figure from a png, jpg, or svg image, convert it in
a QPixmap, and force a repaint of the widget.
"""
self.fig = fig
self.fmt = fmt
if fmt in ['image/png', 'image/jpeg']:
self._qpix_orig = QPixmap()
self._qpix_orig.loadFromData(fig, fmt.upper())
elif fmt == 'image/svg+xml':
self._qpix_orig = QPixmap(svg_to_image(fig))
self._qpix_buffer = [self._qpix_orig]
self.fwidth = self._qpix_orig.width()
self.fheight = self._qpix_orig.height()
def paintEvent(self, event):
"""Qt method override to paint a custom image on the Widget."""
super(FigureCanvas, self).paintEvent(event)
# Prepare the rect on which the image is going to be painted :
fw = self.frameWidth()
rect = QRect(0 + fw, 0 + fw,
self.size().width() - 2 * fw,
self.size().height() - 2 * fw)
if self.fig is None:
return
# Check/update the qpixmap buffer :
qpix2paint = None
for qpix in self._qpix_buffer:
if qpix.size().width() == rect.width():
qpix2paint = qpix
break
else:
if self.fmt in ['image/png', 'image/jpeg']:
qpix2paint = self._qpix_orig.scaledToWidth(
rect.width(), mode=Qt.SmoothTransformation)
elif self.fmt == 'image/svg+xml':
qpix2paint = QPixmap(svg_to_image(self.fig, rect.size()))
self._qpix_buffer.append(qpix2paint)
if qpix2paint is not None:
# Paint the image on the widget :
qp = QPainter()
qp.begin(self)
qp.drawPixmap(rect, qpix2paint)
qp.end()