本文整理汇总了Python中PySide.QtGui.QImage类的典型用法代码示例。如果您正苦于以下问题:Python QImage类的具体用法?Python QImage怎么用?Python QImage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QImage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_color_map
def export_color_map(self):
name = QtGui.QFileDialog.getSaveFileName(self,
'Export Colormap', filter = IMAGE_SAVE_FILTER)[0]
if not name:
return
color_image = QImage(512, 512, QImage.Format_ARGB32)
color_lines = []
height_found = []
for y in xrange(0, 512):
color_lines.append(color_image.scanLine(y))
height_found.append([])
for x in xrange(0, 512):
height_found[y].append(False)
progress = progress_dialog(self.edit_widget, 0, 63, 'Exporting Colormap...')
for z in xrange(0, 64):
if progress.wasCanceled():
break
progress.setValue(z)
image = self.layers[z]
for y in xrange(0, 512):
image_line = image.scanLine(y)
color_line = color_lines[y]
for x in xrange(0, 512):
if height_found[y][x] is False:
s = x * 4
image_pixel = image_line[s:s + 4]
if image_pixel != TRANSPARENT_PACKED:
height_found[y][x] = True
color_line[s:s + 4] = image_pixel
color_image.save(name)
示例2: set_clipboard_image
def set_clipboard_image(self):
"""Export the formatted output to an image and store it in the
clipboard.
The image stored in the clipboard is a PNG file with alpha
transparency.
"""
div = self.view.page().mainFrame().findFirstElement('.output')
images = {}
for background in (Qt.transparent, Qt.white):
image = QImage(div.geometry().size(),
QImage.Format_ARGB32_Premultiplied)
image.fill(background)
painter = QPainter(image)
div.render(painter)
painter.end()
images[background] = image
# Windows needs this buffer hack to get alpha transparency in
# the copied PNG.
buffer_ = QBuffer()
buffer_.open(QIODevice.WriteOnly)
images[Qt.transparent].save(buffer_, 'PNG')
buffer_.close()
data = QMimeData()
data.setData('PNG', buffer_.data())
data.setImageData(images[Qt.white])
QApplication.clipboard().setMimeData(data)
示例3: capture
def capture(self, region=None, selector=None,
format=QImage.Format_ARGB32_Premultiplied):
"""Returns snapshot as QImage.
:param region: An optional tuple containing region as pixel
coodinates.
:param selector: A selector targeted the element to crop on.
:param format: The output image format.
"""
if region is None and selector is not None:
region = self.region_for_selector(selector)
if region:
x1, y1, x2, y2 = region
w, h = (x2 - x1), (y2 - y1)
image = QImage(QSize(x2, y2), format)
painter = QPainter(image)
self.main_frame.render(painter)
painter.end()
image = image.copy(x1, y1, w, h)
else:
self.main_frame.setScrollBarPolicy(QtCore.Qt.Vertical,
QtCore.Qt.ScrollBarAlwaysOff)
self.main_frame.setScrollBarPolicy(QtCore.Qt.Horizontal,
QtCore.Qt.ScrollBarAlwaysOff)
self.page.setViewportSize(self.main_frame.contentsSize())
image = QImage(self.page.viewportSize(), format)
painter = QPainter(image)
self.main_frame.render(painter)
painter.end()
return image
示例4: __init__
class HeroShipView:
""" Represents the Graphical view of the Hero's ship """
def __init__(self, ship_model):
""" Initialize the ship view """
self.ship_model = ship_model
self.scaled_ship = None
def loadShipImage(self, width, height):
""" Load the ship image """
self.unscaled_ship = QImage("hero_ship.png")
matrix = QMatrix()
matrix = matrix.rotate(180)
self.unscaled_ship = self.unscaled_ship.transformed(matrix)
xScaledSize = width*self.ship_model.rectangle.width/100
yScaledSize = height*self.ship_model.rectangle.height/100
self.scaled_ship = self.unscaled_ship.scaled(xScaledSize, yScaledSize)
def draw(self, painter, window):
""" Draw the image """
if self.scaled_ship is None:
self.loadShipImage(window.contentsRect().width(), window.contentsRect().height())
painter.drawImage(self.ship_model.rectangle.x*window.contentsRect().width()/100, self.ship_model.rectangle.y*window.contentsRect().height()/100, self.scaled_ship)
示例5: export_height_map
def export_height_map(self):
name = QtGui.QFileDialog.getSaveFileName(self,
'Export Heightmap', filter = IMAGE_SAVE_FILTER)[0]
if not name:
return
height_packed = []
for z in xrange(0, 64):
height = (63 - z) * 4
height_packed.append(struct.pack('I', QtGui.qRgba(height, height, height, 255)))
height_image = QImage(512, 512, QImage.Format_ARGB32)
height_lines = []
height_found = []
for y in xrange(0, 512):
height_lines.append(height_image.scanLine(y))
height_found.append([])
for x in xrange(0, 512):
height_found[y].append(False)
progress = progress_dialog(self.edit_widget, 0, 63, 'Exporting Heightmap...')
for z in xrange(0, 64):
if progress.wasCanceled():
break
progress.setValue(z)
packed_value = height_packed[z]
image = self.layers[z]
for y in xrange(0, 512):
image_line = image.scanLine(y)
height_line = height_lines[y]
for x in xrange(0, 512):
if height_found[y][x] is False:
s = x * 4
if image_line[s:s + 4] != TRANSPARENT_PACKED:
height_found[y][x] = True
height_line[s:s + 4] = packed_value
height_image.save(name)
示例6: render
def render(self, fileName, width, height):
self.setViewportSize(QSize(width, height))
fileInfo = QFileInfo(fileName)
dir = QDir()
dir.mkpath(fileInfo.absolutePath())
viewportSize = self.viewportSize()
pageSize = self.mainFrame().contentsSize()
if pageSize.isEmpty():
return False
buffer = QImage(pageSize, QImage.Format_ARGB32)
buffer.fill(qRgba(255, 255, 255, 0))
p = QPainter(buffer)
p.setRenderHint( QPainter.Antialiasing, True)
p.setRenderHint( QPainter.TextAntialiasing, True)
p.setRenderHint( QPainter.SmoothPixmapTransform, True)
self.setViewportSize(pageSize)
self.mainFrame().render(p)
p.end()
self.setViewportSize(viewportSize)
return buffer.save(fileName)
示例7: __loadCrop
def __loadCrop(self, loadCrop=True):
name_wo_ext = self.__fileNameWoExt()
json_data = open(name_wo_ext + '.json').read()
image = QImage()
if loadCrop:
image.load(name_wo_ext + '.crop', 'PNG')
return image, json.loads(json_data)
示例8: convert_bitmap
def convert_bitmap(image, width=None, height=None):
pix = None
if isinstance(image, ImageResource):
pix = traitsui_convert_bitmap(image)
elif isinstance(image, Image):
# image = image.convert('RGBA')
data = image.tostring('raw', 'RGBA')
im = QImage(data, image.size[0], image.size[1], QImage.Format_ARGB32)
pix = QPixmap.fromImage(QImage.rgbSwapped(im))
else:
s = image.shape
if len(s) >= 2:
# im = QImage(image.tostring(),s[1], s[0], QImage.Format_RGB888)
im = QImage(image, s[1], s[0], QImage.Format_RGB888)
pix = QPixmap.fromImage(QImage.rgbSwapped(im))
else:
pix = QPixmap()
if pix:
if width > 0 and height > 0:
pix = pix.scaled(width, height)
elif width > 0:
pix = pix.scaledToWidth(width)
if height > 0:
pix = pix.scaledToHeight(height)
return pix
示例9: saveBMC
def saveBMC(self):
"""
.. TODO:: Save a Brother PEL image (An 8bpp, 130x113 pixel monochromatic? bitmap image) Why 8bpp when only 1bpp is needed?
.. TODO:: Should BMC be limited to ~32KB or is this a mix up with Bitmap Cache?
.. TODO:: Is there/should there be other embedded data in the bitmap besides the image itself?
.. NOTE:: Can save a Singer BMC image (An 8bpp, 130x113 pixel colored bitmap image)
"""
# TODO: figure out how to center the image, right now it just plops it to the left side.
img = QImage(150, 150, QImage.Format_ARGB32_Premultiplied)
img.fill(qRgb(255, 255, 255))
extents = self.gscene.itemsBoundingRect() # QRectF
painter = QPainter(img)
targetRect = QRectF(0, 0, 150, 150)
if self.mainWin.getSettingsPrintingDisableBG(): # TODO: Make BMC background into it's own setting?
brush = self.gscene.backgroundBrush() # QBrush
self.gscene.setBackgroundBrush(Qt.NoBrush)
self.gscene.update()
self.gscene.render(painter, targetRect, extents, Qt.KeepAspectRatio)
self.gscene.setBackgroundBrush(brush)
else:
self.gscene.update()
self.gscene.render(painter, targetRect, extents, Qt.KeepAspectRatio)
img.convertToFormat(QImage.Format_Indexed8, Qt.ThresholdDither | Qt.AvoidDither).save("test.bmc", "BMP")
示例10: copy_to_clipboard
def copy_to_clipboard(plot):
# pass
# @staticmethod
# def wx_copy_to_clipboard(plot):
# # WX specific, though QT implementation is similar using
# # QImage and QClipboard
# import wx
#
width, height = plot.outer_bounds
#
gc = PlotGraphicsContext((width, height), dpi=72)
backbuffer = plot.use_backbuffer
plot.use_backbuffer = False
gc.render_component(plot)
plot.use_backbuffer = backbuffer
#
# # Create a bitmap the same size as the plot
# # and copy the plot data to it
bmp = gc.bmp_array
if gc.format().startswith('bgra'):
bmp_rgba = bmp[:,:,[2,1,0,3]]
else:
bmp_rgba = bmp
bitmap=QImage(bmp_rgba.tostring(),width,height, PySide.QtGui.QImage.Format_RGB32)
if QApplication.clipboard():
QApplication.clipboard().setImage(bitmap.copy())
else:
PySide.QtGui.QMessageBox("Unable to open the clipboard.", "Error")
示例11: __init__
def __init__(self, circuit):
super(CircuitItem, self).__init__()
self.setFlag(QGraphicsItem.ItemIsMovable)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
imgDir = filePath('icons/')
self.data = circuit
"""The real info. The class CircuitItem is just a graphical container
around it. data is saved / loaded to / from file.
"""
self.image = QImage(imgDir + circuit.__class__.__name__ + '.png')
"""The graphical representation of our item on screen."""
if not self.image:
self.image = QImage(imgDir + 'Default.png')
self.showCategory = True
self.showName = True
"""Is the item's name shown on screen?"""
self.showCategory = False
"""Is the item's category (circuit class) shown on screen?"""
self.name = QGraphicsSimpleTextItem(self)
# that won't rotate when the PlugItem is rotated by the user.
self.name.setFlag(QGraphicsItem.ItemIgnoresTransformations)
self.name.setText(self.data.name)
self.category = QGraphicsSimpleTextItem(self)
self.category.setFlag(QGraphicsItem.ItemIgnoresTransformations)
self.category.setText(self.data.category)
self.setupPaint()
示例12: LoadImage
def LoadImage(imageFilename, scaledXSize=None, scaledYSize=None):
""" Loads the Image and scales it if necessary """
unscaled_image = QImage(imageFilename)
if scaledXSize is not None and scaledYSize is not None:
return unscaled_image.scaled(scaledXSize, scaledYSize)
else:
return unscaled_image
示例13: SetImageTransparency
def SetImageTransparency(image, transparencyPercentage):
""" Set the Image Transparency to the value provided """
alpha = QImage(image)
painter = QPainter(alpha)
alphaValue = int(transparencyPercentage*255/100)
painter.fillRect(alpha.rect(), QColor(alphaValue, alphaValue, alphaValue))
painter.end()
image.setAlphaChannel(alpha)
示例14: paint
def paint(self, painter, option, widget=None):
painter_inverted = QPainter()
brect= QGraphicsProxyWidget.boundingRect(self)
invertedColor = QImage(brect.width(),brect.height(),QImage.Format_RGB32)
painter_inverted.begin(invertedColor)
QGraphicsProxyWidget.paint(self,painter_inverted, option, widget)
painter_inverted.end()
painter.drawImage(0,0,invertedColor.rgbSwapped())
示例15: rotateImage
def rotateImage(self,filepath):
print "ROTATING FILE: " + filepath
img = QImage(filepath)
rot = QTransform()
rot = rot.rotate(90)
img = img.transformed(rot)
img.save(filepath)
self.imageRotated.emit(filepath)