如何使用Python和Wand获得正确的图像深度?

时间:2015-03-24 19:43:46

标签: python imagemagick png wand

我不是一个图像压缩大师,但我正在寻找图像深度。下面的Python片段显示的图像深度为8,但其他(更可靠)方法表明深度实际为32。

url="http://lesschwab.com/images/product-wizard-ad-tires.png"
width=177, height=177, depth=8, type=truecolormatte, colorspace=srgb

我怀疑基于通道数或色彩空间或其他东西的乘数。如何查找或计算实际图像深度?

#!/usr/bin/env python
from __future__ import print_function
import sys
import requests
from wand.image import Image

def main():
url = 'http://lesschwab.com/images/product-wizard-ad-tires.png'
resp = requests.get(url, timeout=5.0, headers={'User-agent': 'Mozilla/4.0'})
if resp.status_code == 200:
    try:
        with Image(blob=resp.content) as img:
            print ('url="%s"' % url)
            print('width=%d, height=%d, depth=%d, type=%s, colorspace=%s' %
                  (img.width, img.height, img.depth, img.type,               
                   img.colorspace))
    except Exception as ex:
        print('Unable to decode this image (%d bytes) format.' % 
              len(resp.content), str(ex))

if __name__ == '__main__':
    sys.exit(main())

编辑:其他信息:

我使用相同的py代码来读取网络上可能出现的每种可能的图像类型,因此我必须以编程方式执行此操作。对于这个特定的图像,我可以在img.channel_depths中看到8的值[' red'],img.channel_depths [' green'],img.channel_depths [' blue&# 39;]& img.channel_depths [' alpha']但还有15个频道深度键,它们都有值。我猜这是一个'sRGB'的色彩空间。意味着映射到alpha,red,green和amp;蓝色通道深度。不幸的是,颜色空间类型似乎有34种可能性:

('undefined', 'rgb', 'gray', 'transparent', 'ohta', 
 'lab', 'xyz', 'ycbcr', 'ycc', 'yiq', 'ypbpr', 'yuv', 'cmyk', 
 'srgb', 'hsb', 'hsl', 'hwb', 'rec601luma', 'rec601ycbcr', 
 'rec709luma', 'rec709ycbcr', 'log', 'cmy', 'luv', 'hcl', 
 'lch', 'lms', 'lchab', 'lchuv', 'scrgb', 'hsi', 'hsv', 
 'hclp', 'ydbdr') 

和19个可能的频道深度键:

['opacity', 'true_alpha', 'gray', 'rgb_channels', 
'yellow', 'sync_channels', 'default_channels', 'alpha', 
'cyan', 'magenta', 'undefined', 'blue', 'index', 
'gray_channels', 'composite_channels', 'green', 
'all_channels', 'black', 'red']

我希望在压缩类型/颜色空间及其相关的通道深度之间找到一个映射表(或者可能只是每个颜色空间的深度乘数)。

1 个答案:

答案 0 :(得分:1)

如果您从命令行运行ImageMagick,您会看到它有4个通道(RGB和Alpha),每个通道都是8位,因此它实际上是一个32位图像。

identify -verbose http://lesschwab.com/images/product-wizard-ad-tires.png

<强>输出

Image: product-wizard-ad-tires.png
  Base filename: product-wizard-ad-tires.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 177x177+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: 8-bit
  Channel statistics:
    Pixels: 31329
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 108.834 (0.426799)
      standard deviation: 75.2048 (0.294921)
      kurtosis: -0.693513
      skewness: 0.588717
      entropy: 0.919078
    Green:
      min: 0 (0)
      max: 255 (1)
      mean: 109.384 (0.428956)
      standard deviation: 75.446 (0.295867)
      kurtosis: -0.710338
      skewness: 0.57681
      entropy: 0.91832
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 111.959 (0.439056)
      standard deviation: 75.1161 (0.294573)
      kurtosis: -0.750359
      skewness: 0.529271
      entropy: 0.920621
    Alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 207.428 (0.813445)
      standard deviation: 98.4505 (0.38608)
      kurtosis: 0.611796
      skewness: 1.60878
      entropy: 0.117561
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 94.4371 (0.370342)
      standard deviation: 81.6743 (0.320291)
      kurtosis: -0.337738
      skewness: 0.734059
      entropy: 0.718895
  Alpha: srgba(255,255,255,0)   #FFFFFF00
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgba(223,223,223,1)
  Matte color: grey74
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 177x177+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-03-24T22:17:04+00:00
    date:modify: 2015-03-24T22:17:04+00:00
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 6
    png:IHDR.color_type: 6 (RGBA)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 177, 177
    png:sRGB: intent=0 (Perceptual Intent)
    png:text: 1 tEXt/zTXt/iTXt chunks were found
    signature: 6a14a3fd98dcf5dc10b846c51331b384c5f50cf55694f104961c476aae87c418
    Software: Adobe ImageReady
  Artifacts:
    filename: http://lesschwab.com/images/product-wizard-ad-tires.png
    verbose: true
  Tainted: False
  Filesize: 60.1KB
  Number pixels: 31.3K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.9.0-10 Q16 x86_64 2015-03-10 http://www.imagemagick.org