Search code examples
pythonimagedata-structuresmp3byte

How do I get the color depth and/or the resolution from a byte string in Python?


^ As above. Is this possible?

I'm using a library to get image data from an mp3 and it returns the data as a byte string but to display the image I need to get the resolution and color depth.

Ideally I would prefer a library written in Python without dependencies so as to make my app as portable as possible.

If it's not too much trouble I would appreciate an example with the answer, thanks.


Solution

  • You can use PIL to work with images in Python. In your case you can do:

    >>> from StringIO import StringIO
    >>> from PIL import Image
    
    >>> im = Image.open(StringIO(image_string))
    
    >>> im.size, im.bits
    <<< ((1654, 1279), 8)