#14 python api-add image constructor for PIL image


This is a patch to add an alternative constructor to the python zbar lib. It allows one to create a zbar.Image directly from a PIL Image object. For example, the following code:
width, height = pil_image.size
raw = pil_image.tostring()
zimage = zbar.Image(width, height, "Y800", raw)

can not be written as:
zimage = zbar.Image.fromPILImage(pil_image)

I think this is slightly nicer, so I'd like to submit the patch for inclusion. I wrote the patch against the version in Debian Squeeze, but I think that the patch should apply pretty easily to the current head in the hg. Please check it out.


  • Warren Turkal

    Warren Turkal - 2011-02-12

    I am attaching what i believe to be a test for the new constructor. However, I don't know how to run the tests properly without installing the lib. Is there a way to test without installing the lib?

  • Warren Turkal

    Warren Turkal - 2011-03-23


  • spadix

    spadix - 2011-03-29

    Thanks for submitting this code!

    Unfortunately, there hasn't been much time for syntax sugar... I would have patched this in, but if you don't mind, I have a few questions first:

    * Why is it required that the image parameter be a PIL Image? Is there a reason to reject other objects that support the required attributes?

    * Is this the intended logic for this check?
    if(ret == NULL && PyErr_Occurred() != NULL) PyErr_SetString(...);
    Why override the current error?

    * Why have that check at all? Which path is expected to violate the error handling convention? At most this should be relegated to an assertion...

    * Why is the image format hardcoded to Y800? What happens when a PIL image with a different format is passed in? Ideally this adaptor should map the supported formats and error out for others...

  • spadix

    spadix - 2011-03-29
    • priority: 5 --> 3
    • assigned_to: nobody --> spadix
  • spadix

    spadix - 2011-03-29

    > Is there a way to test without installing the lib?

    Yes, I usually run the test suite from a build directory (also usually VPATH for me). You just need to get the module and library locations in the appropriate paths. I usually set the module path explicitly and let libtool handle the other, eg:

    $ PYTHONPATH=python/.libs ./libtool --mode=execute python $(abs_top_srcdir)/python/test/test_zbar.py

    hope that helps...


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks