0
votes

I am not much of an image compression guru but I am looking for the image depth. The Python snippet below shows an image depth of 8 but other (more reliable) methods indicate that the depth is actually 32.

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

I suspect a multiplier based on number of channels or colorspace or something else. How do I find or calculate the actual image depth?

#!/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())

EDIT: Additional info:

I am using the same py code to read every possible image type that may arise on the web so I have to do this pro-grammatically. For this particular image I can see the values of 8 in img.channel_depths['red'], img.channel_depths['green'], img.channel_depths['blue'] & img.channel_depths['alpha'] but there are 15 more channel depth keys and they all have values. I am guessing that a colorspace of 'sRGB' implies a mapping to the alpha, red, green & blue channel depths. Unfortunately there seems to be 34 possibilities for colorspace types:

('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') 

and 19 possible channel depth keys:

['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']

I am hoping find a mapping table between the compression type/colorspace and their associated channel depths (or maybe just a depth multiplier for each colorspace).

1
You just need to add up the elements of the channel_depth[] array - in this case 4x8=32Mark Setchell
There are way more than 4 elements in the channel_depth array and if I add up all the elements it will be well over 32. I found a T/F img.alpha flag. My guess is that if the colorspace is 'rgb' or 'srgb' then I add up the channel_depths for just the 'red', 'green' and 'blue'. If img.alpha is true then I also add the channel_depth['alpha']. Same principle for other colorspaces ('cmy', cmyk', 'gray'). I guess I will run some empirical tests, find the common color-spaces and see how this works out.WigglyWorld
Personally I don't use the Python interface but I find it very hard to believe you need look at the colourspace to determine the bit-depth - they are pretty independent of each other.Mark Setchell
Ran a few tests. You are right - colorspace is unrelated. The python Wand getter always returns a depth of 8 for everything.WigglyWorld

1 Answers

1
votes

If you run ImageMagick from the command-line like this, you will see it has 4 channels (RGB and Alpha) each of which are 8-bit, so it is in effect a 32-bit image.

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

Output

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