【问题标题】:PIL Loading a Transparent PNG as RGB?PIL将透明PNG加载为RGB?
【发布时间】:2017-02-14 02:08:39
【问题描述】:

我正在尝试使用 PIL 合成一些航拍图像并遇到了一些麻烦。我使用 PIL 来加载 this image 的代码:

composite = Image.new('RGBA', (256, 256))
url = 'http://...'
resp = requests.get(url)
content = StringIO(resp.content)
image = Image.open(content)
composite.paste(image, (0, 0), image)

当我拨打composite.paste() 时,PIL 给了我错误“ValueError:透明蒙版错误”。当我打印image.mode 时,果然是RGB 而不是预期的RGBApaste() 需要)。

我下载的 PNG 的 alpha 通道在哪里?

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    下面的代码对我有用:

    from PIL import Image
    import requests
    import StringIO
    
    url = "https://gis.apfo.usda.gov/arcgis/rest/services/NAIP/Tennessee_2016_60cm/ImageServer/exportImage?bbox=-87.1875,34.3071438563,-84.375,36.5978891331&bboxSR=4326&size=256,256&imageSR=102113&transparent=true&format=png&f=image"
    resp = requests.get(url)
    content = StringIO.StringIO(resp.content)
    image = Image.open(content)
    image = image.convert('RGBA')
    composite = Image.new("RGBA", image.size, (255,255,255,0))
    composite.paste(image )
    

    【讨论】:

    • 谢谢,关键是"image.convert('RGBA')"。
    【解决方案2】:

    据我所知,图片是RGBA。看看结果

    $ convert exportImage.png -verbose info
    
    Image: exportImage.png
      Format: PNG (Portable Network Graphics)
      Mime type: image/png
      Class: DirectClass
      Geometry: 256x256+0+0
      Units: Undefined
      Type: TrueColorAlpha
      Endianess: Undefined
      Colorspace: sRGB
      Depth: 8-bit
      Channel depth:
        red: 8-bit
        green: 8-bit
        blue: 8-bit
        alpha: 1-bit
    
    Channel statistics:
        Pixels: 65536
        Red:
          min: 39 (0.152941)
          max: 253 (0.992157)
          mean: 133.019 (0.521641)
          standard deviation: 75.3931 (0.295659)
          kurtosis: -1.05835
          skewness: 0.86831
        Green:
          min: 59 (0.231373)
          max: 253 (0.992157)
          mean: 152.489 (0.597998)
          standard deviation: 62.9948 (0.247038)
          kurtosis: -1.03932
          skewness: 0.87929
        Blue:
          min: 39 (0.152941)
          max: 253 (0.992157)
          mean: 135.817 (0.532617)
          standard deviation: 72.8752 (0.285785)
          kurtosis: -1.01856
          skewness: 0.929917
        Alpha:
          min: 0 (0)
          max: 255 (1)
          mean: 185.273 (0.726562)
          standard deviation: 113.659 (0.445723)
          kurtosis: -0.966513
          skewness: 1.01661
      Image statistics:
        Overall:
          min: 0 (0)
          max: 255 (1)
          mean: 122.763 (0.481424)
          standard deviation: 83.4891 (0.327408)
          kurtosis: -0.482848
          skewness: 0.432253
      Alpha: srgba(253,253,253,0)   #FDFDFD00  --------> (1)
    

    等等。

    现在,让我们使用PIL 和 Python 3 加载图像:

    from PIL import Image
    from io import BytesIO
    
    url = "..."
    resp = requests.get(url)
    cont = BytesIO(resp.content)
    img = Image.open(cont)
    comp = Image.new('RGBA', (256, 256), (255, 255, 255, 0))  # 0 for transparency
    comp.paste(img, (0, 0))
    

    这是因为上面图像统计中的 eqn (1) 我们必须在制作新图像时也提供0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-28
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      相关资源
      最近更新 更多