PIL Image array interface has the wrong size for YCbCr

David Coles <[email protected]>
Newsgroups gmane.comp.python.image
Message-ID <1286513509.2785.71.camel@krikkit>
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  -  [email protected]
http://mail.python.org/mailman/listinfo/image-sig
PIL-ycbcr.patch (text/x-patch, 325 B)
--- /usr/lib/python2.6/dist-packages/PIL/Image.py	2009-11-16 02:51:25.000000000 +1100
+++ Image.py	2010-10-08 15:21:34.289858657 +1100
@@ -213,7 +213,7 @@
     "RGBX": ('|u1', 4),
     "RGBA": ('|u1', 4),
     "CMYK": ('|u1', 4),
-    "YCbCr": ('|u1', 4),
+    "YCbCr": ('|u1', 3),
 }
 
 def _conv_type_shape(im):
signature.asc (application/pgp-signature, 198 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEABECAAYFAkyuo2AACgkQ/R/ZI1P0XTLhFgCfSesX1jCEw0ld77uPmy3aiyZv
1MQAoJ1XygXjv00ELCE4Fe+aw4wOMXaU
=4SGG
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.