PIL: problem to convert an image array to PIL format

Discussion in 'Python' started by Sverre, Dec 17, 2009.

  1. Sverre

    Sverre Guest

    After converting a PIL image in memory to an array with numpy.asarray
    (), I make a adthreshold() with pymorph() with the result, that all
    pixels in the array are either false or true (boolean). But my try to
    convert this back into PIL format is failing

    img = Image.fromarray(rawimg, '1')

    because a true will be interpreted as integer 1 ), so that 7 pixels
    are black and one white. Has someone a solution, so that a picture
    inly with "true" values doesn't look like this?

    http://img707.imageshack.us/img707/6051/p012.jpg
     
    Sverre, Dec 17, 2009
    #1
    1. Advertising

  2. Sverre

    Peter Otten Guest

    Sverre wrote:

    > After converting a PIL image in memory to an array with numpy.asarray
    > (), I make a adthreshold() with pymorph() with the result, that all
    > pixels in the array are either false or true (boolean). But my try to
    > convert this back into PIL format is failing
    >
    > img = Image.fromarray(rawimg, '1')
    >
    > because a true will be interpreted as integer 1 ), so that 7 pixels
    > are black and one white. Has someone a solution, so that a picture
    > inly with "true" values doesn't look like this?
    >
    > http://img707.imageshack.us/img707/6051/p012.jpg


    This has come up before, see

    http://mail.python.org/pipermail/python-list/2009-October/1221578.html

    Image.fromarray() expects one bit per pixel but actually gets one byte. One
    possible workaround: introduce an intermediate array with a format
    understood by fromarray():

    >>> import numpy
    >>> from PIL import Image
    >>> rawimg = numpy.zeros((20, 20), bool)
    >>> rawimg[:10, :10] = rawimg[10:, 10:] = True
    >>> b = numpy.array(rawimg, numpy.uint8)
    >>> b *= 255
    >>> Image.fromarray(b).save("tmp.jpg")


    Peter
     
    Peter Otten, Dec 17, 2009
    #2
    1. Advertising

  3. Sverre

    Sverre Guest

    Sverre, Dec 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,291
    Roedy Green
    May 10, 2004
  2. Daniel Mark
    Replies:
    1
    Views:
    752
    Fredrik Lundh
    Sep 25, 2006
  3. Replies:
    1
    Views:
    809
    mlimber
    Sep 11, 2006
  4. bharath
    Replies:
    4
    Views:
    3,274
    Thomas Fritsch
    Mar 16, 2007
  5. Andrew
    Replies:
    0
    Views:
    545
    Andrew
    Apr 5, 2007
Loading...

Share This Page