当前位置: 首页>>代码示例>>Python>>正文


Python QBuffer.getvalue方法代码示例

本文整理汇总了Python中PyQt5.Qt.QBuffer.getvalue方法的典型用法代码示例。如果您正苦于以下问题:Python QBuffer.getvalue方法的具体用法?Python QBuffer.getvalue怎么用?Python QBuffer.getvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.Qt.QBuffer的用法示例。


在下文中一共展示了QBuffer.getvalue方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: image_to_data

# 需要导入模块: from PyQt5.Qt import QBuffer [as 别名]
# 或者: from PyQt5.Qt.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 and hasattr(QImageWriter, "setOptimizedWrite"):
            w.setOptimizedWrite(True)
        if jpeg_progressive and hasattr(QImageWriter, "setProgressiveScanWrite"):
            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()
开发者ID:kovidgoyal,项目名称:calibre,代码行数:45,代码来源:img.py


注:本文中的PyQt5.Qt.QBuffer.getvalue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。