【问题标题】:How to change image format without writing it to disk using Python Pillow如何使用 Python Pillow 更改图像格式而不将其写入磁盘
【发布时间】:2017-02-14 15:04:30
【问题描述】:

我从网上得到的枕头图片:

response= urllib2.urlopen(<url to gif image>)
img = Image.open(cStringIO.StringIO(response.read()))

我想将它与 tesserocr 一起使用,但它不适用于 GIF 图像。

如果我将图像保存为 PNG img.save("tmp.png") 并加载它img = Image.open("tmp.png") 一切正常。

有没有办法在不写入磁盘的情况下进行这种转换?

【问题讨论】:

    标签: python python-2.7 pillow cstringio


    【解决方案1】:
    import io
    from PIL import Image
    
    
    def convertImageFormat(imgObj, outputFormat=None):
        """Convert image format
        Args:
            imgObj (Image): the Pillow Image instance
            outputFormat (str): Image format, eg: "JPEG"/"PNG"/"BMP"/"TIFF"/...
                more refer: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
        Returns:
            bytes, binary data of Image
        Raises:
        """
        newImgObj = imgObj
        if outputFormat and (imgObj.format != outputFormat):
            imageBytesIO = io.BytesIO()
            imgObj.save(imageBytesIO, outputFormat)
            newImgObj = Image.open(imageBytesIO)
    
    
        return newImgObj
    

    调用示例:

    pngImgFile = "xxx.png"
    pngImgObj = Image.open(pngImgFile)
    convertToFormat = "JPEG"
    convertedJpgImgBytes = convertImageFormat(pngImgObj, convertToFormat)
    

    高级版convertImageFormat可以参考我的库crifanPillow.py

    import io
    from PIL import Image
    
    
    def convertImageFormat(imgObj, outputFormat=None, isOptimize=False, isKeepPrevValues=True):
        """Convert image format
        Args:
            imgObj (Image): the Pillow Image instance
            outputFormat (str): Image format, eg: "JPEG"/"PNG"/"BMP"/"TIFF"/...
                more refer: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
            isOptimize (bool): do optimize when using save to convert format
            isKeepPrevValues (bool): keep previous property values, such as: filename
        Returns:
            bytes, binary data of Image
        Raises:
        """
        newImgObj = imgObj
        if outputFormat and (imgObj.format != outputFormat):
            imageBytesIO = io.BytesIO()
            if isOptimize:
                imgObj.save(imageBytesIO, outputFormat, optimize=True)
            else:
                imgObj.save(imageBytesIO, outputFormat)
            newImgObj = Image.open(imageBytesIO)
            if isKeepPrevValues:
                if imgObj.filename:
                    newImgObj.filename = imgObj.filename
    
    
        return newImgObj
    

    【讨论】:

      【解决方案2】:

      解决方法很简单:

      response= urllib2.urlopen(<url to gif image>)
      img = Image.open(cStringIO.StringIO(response.read()))
      img = img.convert("RGB")
      

      请注意,您需要删除 alpha 通道信息以使图像与 tesserocr 兼容

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 2015-07-09
        • 2019-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-13
        相关资源
        最近更新 更多