Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PIL: problem to convert an image array to PIL format

Reply
Thread Tools

PIL: problem to convert an image array to PIL format

 
 
Sverre
Guest
Posts: n/a
 
      12-17-2009
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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-17-2009
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/pyt...r/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.uint
>>> b *= 255
>>> Image.fromarray(b).save("tmp.jpg")


Peter
 
Reply With Quote
 
 
 
 
Sverre
Guest
Posts: n/a
 
      12-17-2009
On 17 Des, 15:45, Peter Otten <(E-Mail Removed)> wrote:
>
> This has come up before, see
>
> http://mail.python.org/pipermail/pyt...r/1221578.html
>


> Peter


Thank you!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL : How to write array to image ??? Martin Python 6 10-05-2009 08:17 PM
Pil Raw Image Viewer-- How to get image mode Andrew Python 0 04-05-2007 08:49 PM
URGENT,how to convert PGM image or raw format to JPG,PNG format, bharath Java 4 03-16-2007 10:52 AM
How to save High Quality JPG image by setting Image module from PIL Daniel Mark Python 1 09-25-2006 05:59 PM
[PIL] is there a downloadable docs for PIL Egor Bolonev Python 2 12-24-2004 11:05 AM



Advertisments