我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用PyQt5.QtCore.QBuffer()。
def fromqimage(im): buffer = QBuffer() buffer.open(QIODevice.ReadWrite) # preserve alha channel with png # otherwise ppm is more friendly with Image.open if im.hasAlphaChannel(): im.save(buffer, 'png') else: im.save(buffer, 'ppm') b = BytesIO() try: b.write(buffer.data()) except TypeError: # workaround for Python 2 b.write(str(buffer.data())) buffer.close() b.seek(0) return Image.open(b)
def fromqimage(im): """ :param im: A PIL Image object, or a file name (given either as Python string or a PyQt string object) """ buffer = QBuffer() buffer.open(QIODevice.ReadWrite) # preserve alha channel with png # otherwise ppm is more friendly with Image.open if im.hasAlphaChannel(): im.save(buffer, 'png') else: im.save(buffer, 'ppm') b = BytesIO() try: b.write(buffer.data()) except TypeError: # workaround for Python 2 b.write(str(buffer.data())) buffer.close() b.seek(0) return Image.open(b)
def capture(self, url): print('--load') self.load(QUrl(url)) self.wait_load() self.show() size = self.page().contentsSize() self.page().view().resize(*[int(s) for s in [size.width(), size.height()]]) print('--take image') image = QImage(800, 800, QImage.Format_ARGB32) painter = QPainter(image) print('--render') self.page().view().render(painter) painter.end() print('Saving QImage') img_bytes = QByteArray() bio = QBuffer(img_bytes) bio.open(QIODevice.WriteOnly) image.save(bio, 'PNG') return img_bytes
def fromqpixmap(im): return fromqimage(im) # buffer = QBuffer() # buffer.open(QIODevice.ReadWrite) # # im.save(buffer) # # What if png doesn't support some image features like animation? # im.save(buffer, 'ppm') # bytes_io = BytesIO() # bytes_io.write(buffer.data()) # buffer.close() # bytes_io.seek(0) # return Image.open(bytes_io)
def update_img(self, img_data): reader = QtGui.QImageReader() buf = QtCore.QBuffer() buf.setData(img_data) reader.setDevice(buf) new_image = reader.read() # scale to window size prev_size = (self.size() if self.pixmap_item.pixmap().isNull() else self.ui.view.viewport().geometry().size()) # store the full pixmap self.pixmap = QtGui.QPixmap.fromImage(new_image) # do the scaling self.scale_pixmap(prev_size) if not self.pixmap_item.scene(): self.scene.addItem(self.pixmap_item)