本文整理汇总了Python中PyQt5.QtCore.QBuffer.getvalue方法的典型用法代码示例。如果您正苦于以下问题:Python QBuffer.getvalue方法的具体用法?Python QBuffer.getvalue怎么用?Python QBuffer.getvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtCore.QBuffer
的用法示例。
在下文中一共展示了QBuffer.getvalue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: image_to_data
# 需要导入模块: from PyQt5.QtCore import QBuffer [as 别名]
# 或者: from PyQt5.QtCore.QBuffer import getvalue [as 别名]
def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level=9, jpeg_optimized=True, jpeg_progressive=False):
'''
Serialize image to bytestring in the specified format.
:param compression_quality: is for JPEG and goes from 0 to 100. 100 being lowest compression, highest image quality
:param png_compression_level: is for PNG and goes from 0-9. 9 being highest compression.
:param jpeg_optimized: Turns on the 'optimize' option for libjpeg which losslessly reduce file size
:param jpeg_progressive: Turns on the 'progressive scan' option for libjpeg which allows JPEG images to be downloaded in streaming fashion
'''
fmt = fmt.upper()
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QBuffer.WriteOnly)
if fmt == 'GIF':
w = QImageWriter(buf, b'PNG')
w.setQuality(90)
if not w.write(img):
raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
from PIL import Image
im = Image.open(BytesIO(ba.data()))
buf = BytesIO()
im.save(buf, 'gif')
return buf.getvalue()
is_jpeg = fmt in ('JPG', 'JPEG')
w = QImageWriter(buf, fmt.encode('ascii'))
if is_jpeg:
if img.hasAlphaChannel():
img = blend_image(img)
# QImageWriter only gained the following options in Qt 5.5
if jpeg_optimized:
w.setOptimizedWrite(True)
if jpeg_progressive:
w.setProgressiveScanWrite(True)
w.setQuality(compression_quality)
elif fmt == 'PNG':
cl = min(9, max(0, png_compression_level))
w.setQuality(10 * (9-cl))
if not w.write(img):
raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
return ba.data()