Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PIL : How to write array to image ???

Reply
Thread Tools

PIL : How to write array to image ???

 
 
Martin
Guest
Posts: n/a
 
      10-03-2009
Dear group

I'm trying to use PIL to write an array (a NumPy array to be exact) to
an image.
Peace of cake, but it comes out looking strange.

I use the below mini code, that I wrote for the purpose. The print of
a looks like expected:

[[ 200. 200. 200. ..., 0. 0. 0.]
[ 200. 200. 200. ..., 0. 0. 0.]
[ 200. 200. 200. ..., 0. 0. 0.]
...,
[ 0. 0. 0. ..., 200. 200. 200.]
[ 0. 0. 0. ..., 200. 200. 200.]
[ 0. 0. 0. ..., 200. 200. 200.]]

But the image looks nothing like that.

Please see the images on:
http://hvidberg.net/Martin/temp/quat_col.png
http://hvidberg.net/Martin/temp/quat_bw.png

or run the code to see them locally.

Please what do I do wrong in the PIL part ???

Martin



import numpy as np
from PIL import Image
from PIL import ImageOps

maxcol = 100
maxrow = 100

a = np.zeros((maxcol,maxrow),float)

for i in range(maxcol):
for j in range(maxrow):
if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
(maxrow/2)):
a[i,j] = 200
else:
a[i,j] = 0

print a

pilImage = Image.fromarray(a,'RGB')
pilImage.save('quat_col.png')
pilImage = ImageOps.grayscale(pilImage)
pilImage.save('quat_bw.png')
 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      10-03-2009
Martin wrote:
> Dear group
>
> I'm trying to use PIL to write an array (a NumPy array to be exact) to
> an image.
> Peace of cake, but it comes out looking strange.
>
> I use the below mini code, that I wrote for the purpose. The print of
> a looks like expected:
>
> [[ 200. 200. 200. ..., 0. 0. 0.]
> [ 200. 200. 200. ..., 0. 0. 0.]
> [ 200. 200. 200. ..., 0. 0. 0.]
> ...,
> [ 0. 0. 0. ..., 200. 200. 200.]
> [ 0. 0. 0. ..., 200. 200. 200.]
> [ 0. 0. 0. ..., 200. 200. 200.]]
>
> But the image looks nothing like that.
>
> Please see the images on:
> http://hvidberg.net/Martin/temp/quat_col.png
> http://hvidberg.net/Martin/temp/quat_bw.png
>
> or run the code to see them locally.
>
> Please what do I do wrong in the PIL part ???
>
> Martin
>
>
>
> import numpy as np
> from PIL import Image
> from PIL import ImageOps
>
> maxcol = 100
> maxrow = 100
>
> a = np.zeros((maxcol,maxrow),float)
>
> for i in range(maxcol):
> for j in range(maxrow):
> if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> (maxrow/2)):
> a[i,j] = 200
> else:
> a[i,j] = 0
>
> print a
>
> pilImage = Image.fromarray(a,'RGB')


You are telling it the wrong mode information. If you want an RGB image, you
need to give it a uint8 array with shape (height, width, 3). Exactly what are
you trying to do? I can't infer that from your code.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-03-2009
Martin wrote:

> Dear group
>
> I'm trying to use PIL to write an array (a NumPy array to be exact) to
> an image.
> Peace of cake, but it comes out looking strange.
>
> I use the below mini code, that I wrote for the purpose. The print of
> a looks like expected:
>
> [[ 200. 200. 200. ..., 0. 0. 0.]
> [ 200. 200. 200. ..., 0. 0. 0.]
> [ 200. 200. 200. ..., 0. 0. 0.]
> ...,
> [ 0. 0. 0. ..., 200. 200. 200.]
> [ 0. 0. 0. ..., 200. 200. 200.]
> [ 0. 0. 0. ..., 200. 200. 200.]]
>
> But the image looks nothing like that.
>
> Please see the images on:
> http://hvidberg.net/Martin/temp/quat_col.png
> http://hvidberg.net/Martin/temp/quat_bw.png
>
> or run the code to see them locally.
>
> Please – what do I do wrong in the PIL part ???
>
> Martin
>
>
>
> import numpy as np
> from PIL import Image
> from PIL import ImageOps
>
> maxcol = 100
> maxrow = 100
>
> a = np.zeros((maxcol,maxrow),float)
>
> for i in range(maxcol):
> for j in range(maxrow):
> if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> (maxrow/2)):
> a[i,j] = 200
> else:
> a[i,j] = 0
>
> print a
>
> pilImage = Image.fromarray(a,'RGB')
> pilImage.save('quat_col.png')
> pilImage = ImageOps.grayscale(pilImage)
> pilImage.save('quat_bw.png')


The PIL seems to copy the array contents directly from memory without any
conversions or sanity check. In your example The float values determine the
gray value of 8 consecutive pixels.

If you want a[i,j] to become the color of the pixel (i, j) you have to use
an array with a memory layout that is compatible to the Image.
Here are a few examples:

>>> import numpy
>>> from PIL import Image


>>> a = numpy.zeros((100, 100), numpy.uint
>>> a[:50, :50] = a[50:, 50:] = 255
>>> Image.fromarray(a).save("tmp1.png")


>>> b = numpy.zeros((100, 100, 3), numpy.uint
>>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
>>> Image.fromarray(b).save("tmp2.png")


>>> c = numpy.zeros((100, 100), numpy.uint32)
>>> c[:50, :50] = c[50:, 50:] = 0xff808000
>>> Image.fromarray(c, "RGBA").save("tmp3.png")


Peter

 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      10-04-2009
On Oct 3, 11:56*pm, Peter Otten <(E-Mail Removed)> wrote:
> Martin wrote:
> > Dear group

>
> > I'm trying to use PIL to write an array (a NumPy array to be exact) to
> > an image.
> > Peace of cake, but it comes out looking strange.

>
> > I use the below mini code, that I wrote for the purpose. The print of
> > a looks like expected:

>
> > [[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > *...,
> > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > *[ * 0. * *0. * *0. ..., *200. *200. *200.]]

>
> > But the image looks nothing like that.

>
> > Please see the images on:
> >http://hvidberg.net/Martin/temp/quat_col.png
> >http://hvidberg.net/Martin/temp/quat_bw.png

>
> > or run the code to see them locally.

>
> > Please what do I do wrong in the PIL part ???

>
> > Martin

>
> > import numpy as np
> > from PIL import Image
> > from PIL import ImageOps

>
> > maxcol = 100
> > maxrow = 100

>
> > a = np.zeros((maxcol,maxrow),float)

>
> > for i in range(maxcol):
> > * * for j in range(maxrow):
> > * * * * if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> > (maxrow/2)):
> > * * * * * * a[i,j] = 200
> > * * * * else:
> > * * * * * * a[i,j] = 0

>
> > print a

>
> > pilImage = Image.fromarray(a,'RGB')
> > pilImage.save('quat_col.png')
> > pilImage = ImageOps.grayscale(pilImage)
> > pilImage.save('quat_bw.png')

>
> The PIL seems to copy the array contents directly from memory without any
> conversions or sanity check. In your example The float values determine the
> gray value of 8 consecutive pixels.
>
> If you want a[i,j] to become the color of the pixel (i, j) you have to use
> an array with a memory layout that is compatible to the Image.
> Here are a few examples:
>
> >>> import numpy
> >>> from PIL import Image
> >>> a = numpy.zeros((100, 100), numpy.uint
> >>> a[:50, :50] = a[50:, 50:] = 255
> >>> Image.fromarray(a).save("tmp1.png")
> >>> b = numpy.zeros((100, 100, 3), numpy.uint
> >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
> >>> Image.fromarray(b).save("tmp2.png")
> >>> c = numpy.zeros((100, 100), numpy.uint32)
> >>> c[:50, :50] = c[50:, 50:] = 0xff808000
> >>> Image.fromarray(c, "RGBA").save("tmp3.png")

>
> Peter


Thanks All - That helped a lot...

The working code ended with:


imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint
for ro in range(imgL.shape[1]):
for co in range(imgL.shape[0]):
imga[ro,co] = imgL[ro,co]
Image.fromarray(imga).save('_a'+str(lev)+'.png')

 
Reply With Quote
 
Mart.
Guest
Posts: n/a
 
      10-04-2009
On Oct 4, 9:47*am, Martin <(E-Mail Removed)> wrote:
> On Oct 3, 11:56*pm, Peter Otten <(E-Mail Removed)> wrote:
>
>
>
>
>
> > Martin wrote:
> > > Dear group

>
> > > I'm trying to use PIL to write an array (a NumPy array to be exact) to
> > > an image.
> > > Peace of cake, but it comes out looking strange.

>
> > > I use the below mini code, that I wrote for the purpose. The print of
> > > a looks like expected:

>
> > > [[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > *...,
> > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]]

>
> > > But the image looks nothing like that.

>
> > > Please see the images on:
> > >http://hvidberg.net/Martin/temp/quat_col.png
> > >http://hvidberg.net/Martin/temp/quat_bw.png

>
> > > or run the code to see them locally.

>
> > > Please what do I do wrong in the PIL part ???

>
> > > Martin

>
> > > import numpy as np
> > > from PIL import Image
> > > from PIL import ImageOps

>
> > > maxcol = 100
> > > maxrow = 100

>
> > > a = np.zeros((maxcol,maxrow),float)

>
> > > for i in range(maxcol):
> > > * * for j in range(maxrow):
> > > * * * * if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> > > (maxrow/2)):
> > > * * * * * * a[i,j] = 200
> > > * * * * else:
> > > * * * * * * a[i,j] = 0

>
> > > print a

>
> > > pilImage = Image.fromarray(a,'RGB')
> > > pilImage.save('quat_col.png')
> > > pilImage = ImageOps.grayscale(pilImage)
> > > pilImage.save('quat_bw.png')

>
> > The PIL seems to copy the array contents directly from memory without any
> > conversions or sanity check. In your example The float values determine the
> > gray value of 8 consecutive pixels.

>
> > If you want a[i,j] to become the color of the pixel (i, j) you have to use
> > an array with a memory layout that is compatible to the Image.
> > Here are a few examples:

>
> > >>> import numpy
> > >>> from PIL import Image
> > >>> a = numpy.zeros((100, 100), numpy.uint
> > >>> a[:50, :50] = a[50:, 50:] = 255
> > >>> Image.fromarray(a).save("tmp1.png")
> > >>> b = numpy.zeros((100, 100, 3), numpy.uint
> > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
> > >>> Image.fromarray(b).save("tmp2.png")
> > >>> c = numpy.zeros((100, 100), numpy.uint32)
> > >>> c[:50, :50] = c[50:, 50:] = 0xff808000
> > >>> Image.fromarray(c, "RGBA").save("tmp3.png")

>
> > Peter

>
> Thanks All - That helped a lot...
>
> The working code ended with:
>
> * * * * imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint
> * * * * for ro in range(imgL.shape[1]):
> * * * * * * for co in range(imgL.shape[0]):
> * * * * * * * * imga[ro,co] = imgL[ro,co]
> * * * * Image.fromarray(imga).save('_a'+str(lev)+'.png')


Without knowing how big your image is (can't remember if you said!).
Perhaps rather than looping in the way you might in C for example, the
numpy where might be quicker if you have a big image. Just a
thought...

 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      10-05-2009
On Oct 4, 10:16*pm, "Mart." <(E-Mail Removed)> wrote:
> On Oct 4, 9:47*am, Martin <(E-Mail Removed)> wrote:
>
>
>
> > On Oct 3, 11:56*pm, Peter Otten <(E-Mail Removed)> wrote:

>
> > > Martin wrote:
> > > > Dear group

>
> > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to
> > > > an image.
> > > > Peace of cake, but it comes out looking strange.

>
> > > > I use the below mini code, that I wrote for the purpose. The print of
> > > > a looks like expected:

>
> > > > [[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > *...,
> > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]]

>
> > > > But the image looks nothing like that.

>
> > > > Please see the images on:
> > > >http://hvidberg.net/Martin/temp/quat_col.png
> > > >http://hvidberg.net/Martin/temp/quat_bw.png

>
> > > > or run the code to see them locally.

>
> > > > Please what do I do wrong in the PIL part ???

>
> > > > Martin

>
> > > > import numpy as np
> > > > from PIL import Image
> > > > from PIL import ImageOps

>
> > > > maxcol = 100
> > > > maxrow = 100

>
> > > > a = np.zeros((maxcol,maxrow),float)

>
> > > > for i in range(maxcol):
> > > > * * for j in range(maxrow):
> > > > * * * * if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> > > > (maxrow/2)):
> > > > * * * * * * a[i,j] = 200
> > > > * * * * else:
> > > > * * * * * * a[i,j] = 0

>
> > > > print a

>
> > > > pilImage = Image.fromarray(a,'RGB')
> > > > pilImage.save('quat_col.png')
> > > > pilImage = ImageOps.grayscale(pilImage)
> > > > pilImage.save('quat_bw.png')

>
> > > The PIL seems to copy the array contents directly from memory without any
> > > conversions or sanity check. In your example The float values determine the
> > > gray value of 8 consecutive pixels.

>
> > > If you want a[i,j] to become the color of the pixel (i, j) you have to use
> > > an array with a memory layout that is compatible to the Image.
> > > Here are a few examples:

>
> > > >>> import numpy
> > > >>> from PIL import Image
> > > >>> a = numpy.zeros((100, 100), numpy.uint
> > > >>> a[:50, :50] = a[50:, 50:] = 255
> > > >>> Image.fromarray(a).save("tmp1.png")
> > > >>> b = numpy.zeros((100, 100, 3), numpy.uint
> > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
> > > >>> Image.fromarray(b).save("tmp2.png")
> > > >>> c = numpy.zeros((100, 100), numpy.uint32)
> > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000
> > > >>> Image.fromarray(c, "RGBA").save("tmp3.png")

>
> > > Peter

>
> > Thanks All - That helped a lot...

>
> > The working code ended with:

>
> > * * * * imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint
> > * * * * for ro in range(imgL.shape[1]):
> > * * * * * * for co in range(imgL.shape[0]):
> > * * * * * * * * imga[ro,co] = imgL[ro,co]
> > * * * * Image.fromarray(imga).save('_a'+str(lev)+'.png')

>
> Without knowing how big your image is (can't remember if you said!).
> Perhaps rather than looping in the way you might in C for example, the
> numpy where might be quicker if you have a big image. Just a
> thought...


And a good thought too... I use to teach ansi C at Univ. many ears
ago, and it's still one of my favorits. But I prefere Python for this
type of work. Python have a much faster develop time, is much easier
to write (fewer linees, simpler syntax) and is much more 'hot' in
these days. Finally my favorit GIS and RS applications support Python
out-of-the-box.

Martin
 
Reply With Quote
 
Mart.
Guest
Posts: n/a
 
      10-05-2009
On Oct 5, 5:14*pm, Martin <(E-Mail Removed)> wrote:
> On Oct 4, 10:16*pm, "Mart." <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Oct 4, 9:47*am, Martin <(E-Mail Removed)> wrote:

>
> > > On Oct 3, 11:56*pm, Peter Otten <(E-Mail Removed)> wrote:

>
> > > > Martin wrote:
> > > > > Dear group

>
> > > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to
> > > > > an image.
> > > > > Peace of cake, but it comes out looking strange.

>
> > > > > I use the below mini code, that I wrote for the purpose. The print of
> > > > > a looks like expected:

>
> > > > > [[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > > *[ 200. *200. *200. ..., * *0. * *0. * *0.]
> > > > > *...,
> > > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]
> > > > > *[ * 0. * *0. * *0. ..., *200. *200. *200.]]

>
> > > > > But the image looks nothing like that.

>
> > > > > Please see the images on:
> > > > >http://hvidberg.net/Martin/temp/quat_col.png
> > > > >http://hvidberg.net/Martin/temp/quat_bw.png

>
> > > > > or run the code to see them locally.

>
> > > > > Please what do I do wrong in the PIL part ???

>
> > > > > Martin

>
> > > > > import numpy as np
> > > > > from PIL import Image
> > > > > from PIL import ImageOps

>
> > > > > maxcol = 100
> > > > > maxrow = 100

>
> > > > > a = np.zeros((maxcol,maxrow),float)

>
> > > > > for i in range(maxcol):
> > > > > * * for j in range(maxrow):
> > > > > * * * * if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> > > > > (maxrow/2)):
> > > > > * * * * * * a[i,j] = 200
> > > > > * * * * else:
> > > > > * * * * * * a[i,j] = 0

>
> > > > > print a

>
> > > > > pilImage = Image.fromarray(a,'RGB')
> > > > > pilImage.save('quat_col.png')
> > > > > pilImage = ImageOps.grayscale(pilImage)
> > > > > pilImage.save('quat_bw.png')

>
> > > > The PIL seems to copy the array contents directly from memory without any
> > > > conversions or sanity check. In your example The float values determine the
> > > > gray value of 8 consecutive pixels.

>
> > > > If you want a[i,j] to become the color of the pixel (i, j) you have to use
> > > > an array with a memory layout that is compatible to the Image.
> > > > Here are a few examples:

>
> > > > >>> import numpy
> > > > >>> from PIL import Image
> > > > >>> a = numpy.zeros((100, 100), numpy.uint
> > > > >>> a[:50, :50] = a[50:, 50:] = 255
> > > > >>> Image.fromarray(a).save("tmp1.png")
> > > > >>> b = numpy.zeros((100, 100, 3), numpy.uint
> > > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
> > > > >>> Image.fromarray(b).save("tmp2.png")
> > > > >>> c = numpy.zeros((100, 100), numpy.uint32)
> > > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000
> > > > >>> Image.fromarray(c, "RGBA").save("tmp3.png")

>
> > > > Peter

>
> > > Thanks All - That helped a lot...

>
> > > The working code ended with:

>
> > > * * * * imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint
> > > * * * * for ro in range(imgL.shape[1]):
> > > * * * * * * for co in range(imgL.shape[0]):
> > > * * * * * * * * imga[ro,co] = imgL[ro,co]
> > > * * * * Image.fromarray(imga).save('_a'+str(lev)+'.png')

>
> > Without knowing how big your image is (can't remember if you said!).
> > Perhaps rather than looping in the way you might in C for example, the
> > numpy where might be quicker if you have a big image. Just a
> > thought...

>
> And a good thought too... I use to teach ansi C at Univ. many ears
> ago, and it's still one of my favorits. But I prefere Python for this
> type of work. Python have a much faster develop time, is much easier
> to write (fewer linees, simpler syntax) and is much more 'hot' in
> these days. Finally my favorit GIS and RS applications support Python
> out-of-the-box.
>
> Martin


I wasn't advocating doing it in c just that you could speed up your
program considerably by not looping and using some of the numpy array
options.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
PIL: problem to convert an image array to PIL format Sverre Python 2 12-17-2009 04:33 PM
Pil Raw Image Viewer-- How to get image mode Andrew Python 0 04-05-2007 08:49 PM
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