FAQ

[Image-SIG] Array to Image transformations

Sahar Vilan
Feb 21, 2008 at 8:47 am
Can anyone help me with transformation from numpy array to image (at PIL
library)?
I want to change an array (at numpy) into an image (at Image) in order to
rotate the image and save it in standard image format.

Tahnks,
Sahar Vilan






*******************************************************************************************************
This e-mail message may contain confidential,and privileged information or data that constitute proprietary information of CMT Medical Ltd. Any review or distribution by others is strictly prohibited. If you are not the intended recipient you are hereby notified that any use of this information or data by any other person is absolutely prohibited. If you are not the intended recipient, please delete all copies. Thank You. http://www.cmt.co.il
********************************************************************************************************







************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/image-sig/attachments/20080221/2759fa5f/attachment.htm
reply

Search Discussions

7 responses

  • Joao S. O. Bueno at Feb 26, 2008 at 1:21 pm

    On Thu 21 Feb 2008 05:47:22 Sahar Vilan wrote:
    Can anyone help me with transformation from numpy array to image (at PIL
    library)?
    I want to change an array (at numpy) into an image (at Image) in order to
    rotate the image and save it in standard image format.

    Tahnks,
    Sahar Vilan
    Hi...

    Sorry for the long delay.
    These convertions in python among data and image representation indeed require
    some experimentation.

    (In my todo list, wiht low priority, I have a python module that would
    seamlessly translate image data across various image handling
    bindings/ibraries and numerical types).

    Fortunatey, in this case it is rather simple:

    For numpy.array -> python image, you do something like:

    a = numpy.array ("\x0" (WIDTH * HEIGHT * 3), "B")
    #code to create imag data inside array
    # (...)
    i = Image.new("RGB", (WIDTH, HEIGHT))

    i.fromstring(a.tostring())
    i.save(filename)


    js
    -><-



    ***************************************************************************
    **************************** This e-mail message may contain
    confidential,and privileged information or data that constitute
    proprietary information of CMT Medical Ltd. Any review or distribution by
    others is strictly prohibited. If you are not the intended recipient you
    are hereby notified that any use of this information or data by any other
    person is absolutely prohibited. If you are not the intended recipient,
    please delete all copies. Thank You. http://www.cmt.co.il
    ***************************************************************************
    *****************************







    ***************************************************************************
    ********* This footnote confirms that this email message has been scanned by
    PineApp Mail-SeCure for the presence of malicious code, vandals & computer
    viruses.
    ***************************************************************************
    *********
  • Frédéric Mantegazza at Feb 27, 2008 at 6:23 am

    On mardi 26 f?vrier 2008, Joao S. O. Bueno wrote:
    On Thu 21 Feb 2008 05:47:22 Sahar Vilan wrote:

    Can anyone help me with transformation from numpy array to image (at
    PIL library)?
    I want to change an array (at numpy) into an image (at Image) in order
    to rotate the image and save it in standard image format.
    Sorry for the long delay.
    These convertions in python among data and image representation indeed
    require some experimentation.

    (In my todo list, wiht low priority, I have a python module that would
    seamlessly translate image data across various image handling
    bindings/ibraries and numerical types).

    Fortunatey, in this case it is rather simple:

    For numpy.array -> python image, you do something like:

    a = numpy.array ("\x0" (WIDTH * HEIGHT * 3), "B")
    #code to create imag data inside array
    # (...)
    i = Image.new("RGB", (WIDTH, HEIGHT))

    i.fromstring(a.tostring())
    i.save(filename)
    Hello,

    I'm also very interested by such features, because I think it could be a
    nice way to use color management libraries. For example, the little cms
    python wrapper could handle arrays (which it does not yet), and then, PIL
    could convert image from/to arrays.
    i.fromstring(a.tostring())
    What is the performances of such transformations?
  • Carlos da Silva Santos at Feb 27, 2008 at 11:12 am
    There are currently two PEPs related to this kind of problem. One is
    lead by Travis Oliphant of numpy and is about establishing a buffer
    protocol for data interchange. The second one is specifically related
    to passing image data between applications/toolkits.

    http://www.python.org/dev/peps/pep-3118/
    http://www.python.org/dev/peps/pep-0368/

    So I guess in the near future we will get rid of string conversions.

    Carlos

    On Wed, Feb 27, 2008 at 6:23 AM, Fr?d?ric Mantegazza
    wrote:
    On mardi 26 f?vrier 2008, Joao S. O. Bueno wrote:
    On Thu 21 Feb 2008 05:47:22 Sahar Vilan wrote:

    Can anyone help me with transformation from numpy array to image (at
    PIL library)?
    I want to change an array (at numpy) into an image (at Image) in order
    to rotate the image and save it in standard image format.
    Sorry for the long delay.
    These convertions in python among data and image representation indeed
    require some experimentation.

    (In my todo list, wiht low priority, I have a python module that would
    seamlessly translate image data across various image handling
    bindings/ibraries and numerical types).

    Fortunatey, in this case it is rather simple:

    For numpy.array -> python image, you do something like:

    a = numpy.array ("\x0" (WIDTH * HEIGHT * 3), "B")
    #code to create imag data inside array
    # (...)
    i = Image.new("RGB", (WIDTH, HEIGHT))

    i.fromstring(a.tostring())
    i.save(filename)
    Hello,

    I'm also very interested by such features, because I think it could be a
    nice way to use color management libraries. For example, the little cms
    python wrapper could handle arrays (which it does not yet), and then, PIL
    could convert image from/to arrays.
    i.fromstring(a.tostring())
    What is the performances of such transformations?

    --
    Fr?d?ric

    http://www.gbiloba.org


    _______________________________________________
    Image-SIG maillist - Image-SIG at python.org
    http://mail.python.org/mailman/listinfo/image-sig
  • Frédéric at Feb 27, 2008 at 12:05 pm

    Le 27/2/2008, "Carlos da Silva Santos" <carlos.s.santos at gmail.com> a ?crit:

    There are currently two PEPs related to this kind of problem. One is
    lead by Travis Oliphant of numpy and is about establishing a buffer
    protocol for data interchange. The second one is specifically related
    to passing image data between applications/toolkits.

    http://www.python.org/dev/peps/pep-3118/
    http://www.python.org/dev/peps/pep-0368/

    So I guess in the near future we will get rid of string conversions.
    Very interesting. Thanks for the links!
  • Christopher Barker at Feb 27, 2008 at 5:48 pm

    Carlos da Silva Santos wrote:
    There are currently two PEPs related to this kind of problem. One is
    lead by Travis Oliphant of numpy and is about establishing a buffer
    protocol for data interchange. The second one is specifically related
    to passing image data between applications/toolkits.

    http://www.python.org/dev/peps/pep-3118/
    http://www.python.org/dev/peps/pep-0368/

    So I guess in the near future we will get rid of string conversions.
    Actually, we already have, for numpy and PIL anyway:


    # Added ?fromarray? function, which takes an object implementing the
    NumPy array interface and creates a PIL Image from it. (from Travis
    Oliphant).

    # Added NumPy array interface support (__array_interface__) to the Image
    class (based on code by Travis Oliphant). This allows you to easily
    convert between PIL image memories and NumPy arrays:

    import numpy, Image

    i = Image.open('lena.jpg')
    a = numpy.asarray(i) # a is readonly
    i = Image.fromarray(a)

    See:

    http://effbot.org/zone/pil-changes-116.htm

    No data copying at all!

    -Chris

    --
    Christopher Barker, Ph.D.
    Oceanographer

    Emergency Response Division
    NOAA/NOS/OR&R (206) 526-6959 voice
    7600 Sand Point Way NE (206) 526-6329 fax
    Seattle, WA 98115 (206) 526-6317 main reception

    Chris.Barker at noaa.gov
  • Frédéric Mantegazza at Feb 27, 2008 at 7:42 pm

    On mercredi 27 f?vrier 2008, Christopher Barker wrote:

    Actually, we already have, for numpy and PIL anyway:


    # Added ?fromarray? function, which takes an object implementing the
    NumPy array interface and creates a PIL Image from it. (from Travis
    Oliphant).

    # Added NumPy array interface support (__array_interface__) to the Image
    class (based on code by Travis Oliphant). This allows you to easily
    convert between PIL image memories and NumPy arrays:

    import numpy, Image

    i = Image.open('lena.jpg')
    a = numpy.asarray(i) # a is readonly
    i = Image.fromarray(a)

    See:

    http://effbot.org/zone/pil-changes-116.htm

    No data copying at all!
    Great! I was using the on-line documentation, which only covers 1.1.5...
    I'll have a look at the 1.1.6 doc.
  • Joao S. O. Bueno at Feb 27, 2008 at 11:29 am

    On Wednesday 27 February 2008, Fr?d?ric Mantegazza wrote:
    On mardi 26 f?vrier 2008, Joao S. O. Bueno wrote:
    On Thu 21 Feb 2008 05:47:22 Sahar Vilan wrote:
    Can anyone help me with transformation from numpy array to
    image (at PIL library)?
    I want to change an array (at numpy) into an image (at Image)
    in order to rotate the image and save it in standard image
    format.
    Sorry for the long delay.
    These convertions in python among data and image representation
    indeed require some experimentation.

    (In my todo list, wiht low priority, I have a python module that
    would seamlessly translate image data across various image
    handling bindings/ibraries and numerical types).

    Fortunatey, in this case it is rather simple:

    For numpy.array -> python image, you do something like:

    a = numpy.array ("\x0" (WIDTH * HEIGHT * 3), "B")
    #code to create imag data inside array
    # (...)
    i = Image.new("RGB", (WIDTH, HEIGHT))

    i.fromstring(a.tostring())
    i.save(filename)
    Hello,

    I'm also very interested by such features, because I think it could
    be a nice way to use color management libraries. For example, the
    little cms python wrapper could handle arrays (which it does not
    yet), and then, PIL could convert image from/to arrays.
    i.fromstring(a.tostring())
    What is the performances of such transformations?
    Hi. I did not measure it, and just tested with a small image.
    I think that computing-wise this is not intensive, but I see this
    could need as much as twice the image size in RAM - one for copying
    the array data to a string, and another to copy the same data again
    to a PIL image.

    It maybe however that pynum.tostring points to the same data in system
    memory, so you just need to have memory for the Image object to be
    created.

    As for CPU performance - this should be fast, as it is simply copying
    data around, and both numpy and PIL should do it the fastest possible
    way. Before finding out both to/from string method I made some
    trials with list compreensions tgo copy color components around. That
    was very slow, even for a 256x256 test image.


    js
    -><-

Related Discussions