FAQ

[Image-SIG] PIL Image array interface has the wrong size for YCbCr

David Coles
Oct 8, 2010 at 4:51 am
PIL's Image class has incorrect dimension specified for YCbCr images.
This causes issues when converting to or from NumPy arrays.

According to http://www.pythonware.com/library/pil/handbook/concepts.htm
YCbCr should be "3x8-bit pixels, colour video format". Instead it
appears to be converted to a 4x8-bit format.

The incorrect definition is at line 206 of
http://svn.effbot.python-hosting.com/pil/PIL/Image.py.

Example: Load up any image and convert to YCbCr.
import numpy
import Image as im
image = im.open('bush640x360.png')
ycbcr = image.convert('YCbCr')
Using the Array interface produces a HxWx4 array, which is the wrong
dimensions for YCbCr. Thus when selecting a single channel it displays
incorrectly:
A = numpy.asarray(ycbcr)
print A.shape
(360, 640, 4)
im.fromarray(A[:,:,0], "L").show()
Here's an example decoding the image byte string ourselves gives the
correct result:
B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1',
ycbcr.tostring())
print B.shape
(360, 640, 3)
im.fromarray(B[:,:,0], "L").show()
Attached is a patch against the 1.1.7-2 (python-imaging) package in
Ubuntu as I can't find the development repository for 1.1.7. See
https://bugs.edge.launchpad.net/ubuntu/+source/python-imaging/+bug/656666 for details.

Cheers,
David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PIL-ycbcr.patch
Type: text/x-patch
Size: 325 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/image-sig/attachments/20101008/31ff4139/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/image-sig/attachments/20101008/31ff4139/attachment.pgp>
reply

Search Discussions

3 responses

  • Fredrik Lundh at Nov 9, 2010 at 1:01 am
    Just committed a fix to trunk (including some simple tests for this
    operation & mode...).

    Thanks! /F
    On Fri, Oct 8, 2010 at 6:51 AM, David Coles wrote:
    PIL's Image class has incorrect dimension specified for YCbCr images.
    This causes issues when converting to or from NumPy arrays.

    According to http://www.pythonware.com/library/pil/handbook/concepts.htm
    YCbCr should be "3x8-bit pixels, colour video format". Instead it
    appears to be converted to a 4x8-bit format.

    The incorrect definition is at line 206 of
    http://svn.effbot.python-hosting.com/pil/PIL/Image.py.

    Example: Load up any image and convert to YCbCr.
    import numpy
    import Image as im
    image = im.open('bush640x360.png')
    ycbcr = image.convert('YCbCr')
    Using the Array interface produces a HxWx4 array, which is the wrong
    dimensions for YCbCr. Thus when selecting a single channel it displays
    incorrectly:
    A = numpy.asarray(ycbcr)
    print A.shape
    (360, 640, 4)
    im.fromarray(A[:,:,0], "L").show()
    Here's an example decoding the image byte string ourselves gives the
    correct result:
    B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1',
    ycbcr.tostring())
    print B.shape
    (360, 640, 3)
    im.fromarray(B[:,:,0], "L").show()
    Attached is a patch against the 1.1.7-2 (python-imaging) package in
    Ubuntu as I can't find the development repository for 1.1.7. See
    https://bugs.edge.launchpad.net/ubuntu/+source/python-imaging/+bug/656666 for details.

    Cheers,
    David

    _______________________________________________
    Image-SIG maillist ?- ?Image-SIG at python.org
    http://mail.python.org/mailman/listinfo/image-sig
  • David Coles at Nov 9, 2010 at 11:08 pm
    Nice!

    Is http://hg.effbot.org/pil-2009-raclette the current trunk? There seems
    to be a bit of confusion of which is the current repository. Quite handy
    when trying to write patches.

    The ones I've seen are:

    http://effbot.org/zone/pil-index.htm links to
    http://svn.effbot.python-hosting.com/pil/

    http://effbot.org/downloads/#imaging links to
    http://svn.effbot.org/public/tags/pil-1.1.7

    http://hg.effbot.org/pil-2009-raclette (and the pil-117 fork) seems to
    be the most up to date.

    Cheers,
    David
    On Tue, 2010-11-09 at 02:01 +0100, Fredrik Lundh wrote:
    Just committed a fix to trunk (including some simple tests for this
    operation & mode...).

    Thanks! /F
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: signature.asc
    Type: application/pgp-signature
    Size: 198 bytes
    Desc: This is a digitally signed message part
    URL: <http://mail.python.org/pipermail/image-sig/attachments/20101110/d66caf9f/attachment.pgp>
  • Fredrik Lundh at Nov 10, 2010 at 3:09 am

    On Wed, Nov 10, 2010 at 12:08 AM, David Coles wrote:
    Nice!

    Is http://hg.effbot.org/pil-2009-raclette the current trunk? There seems
    to be a bit of confusion of which is the current repository. Quite handy
    when trying to write patches.

    The ones I've seen are:

    http://effbot.org/zone/pil-index.htm links to
    http://svn.effbot.python-hosting.com/pil/
    Ouch, that's ancient. I thought that repository was deleted ages ago.
    I'll fix the link asap, and figure out if I can get the
    python-hosting folks to nuke the repo.
    http://effbot.org/downloads/#imaging links to
    http://svn.effbot.org/public/tags/pil-1.1.7
    That's a SVN snapshot of the latest release, for SVN users.
    http://hg.effbot.org/pil-2009-raclette (and the pil-117 fork) seems to
    be the most up to date.
    Yeah, the raclette HG repo is where the action is. I agree that the
    name is a bit confusing... :)

    </F>

Related Discussions

Discussion Navigation
viewthread | post

2 users in discussion

David Coles: 2 posts Fredrik Lundh: 2 posts