Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PIL: problem to convert an image array to PIL format

45 views
Skip to first unread message

Sverre

unread,
Dec 17, 2009, 7:14:20 AM12/17/09
to
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

Peter Otten

unread,
Dec 17, 2009, 9:45:34 AM12/17/09
to
Sverre wrote:

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

Sverre

unread,
Dec 17, 2009, 11:33:44 AM12/17/09
to
On 17 Des, 15:45, Peter Otten <__pete...@web.de> wrote:
>
> This has come up before, see
>
> http://mail.python.org/pipermail/python-list/2009-October/1221578.html
>

> Peter

Thank you!

0 new messages