[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

do i need to create new rgbimage class

jimgardener

12/29/2007 3:43:00 PM

hi
am a beginner in python and PIL .I need to read an RGB 8 bit image
(am using jpeg )and pack the rgb color values into a double value so i
can store the image as a list of pixelvalues.From my application i
should be able to call rgbimage1.getpixellist() to retrieve the double
values of an image.
Do i need to create a new class for this?I made something like

class myrgbimage:
def __init__(self,filename):

def _readimage(self):
im=Image.open(filename)
self._readImage(filename)
self._wd,self._ht=im.size
for y in range(self._ht):
for x in range(self._wd):
r,g,b=im.getpixel((x,y))
pval=self.rgbTodoubleval((r,g,b))
self._pixellist.append(pval)

def rgbTodoubleval(self,(r,g,b)):
alpha=255
pixelvalue=(alpha<<24)|(r<<16 )|( g<<8) | b
return pixelvalue

the way i am currently using this is to create instances using
filenames and then retrieving the list of pixelvalues in the image.
like
for z in imagefilenamelist:
myimage=myrgbimage(z)
imagelist.append(z)

so later on i can take each instance and get its width,height and
pixellist and work with them..anyway the code takes too much time and
I wish to know if i can get these 'packed pixelvalues' straight away
without using the above class

jim
4 Answers

Gabriel Genellina

12/30/2007 7:58:00 AM

0

On 29 dic, 12:42, jimgarde...@gmail.com wrote:

> am a beginner in python and PIL  .I need to read an RGB 8 bit image
> (am using jpeg )and pack the rgb color values into a double value so i
> can store the image as a list of pixelvalues.From my application i
> should be able to call rgbimage1.getpixellist() to retrieve the double
> values of an image.

(May I ask why an accessor like getpixellist() instead of simply
rgbimage1.pixellist?)

> Do i need to create a new class for this?I made something like
>
> class myrgbimage:
>       def  __init__(self,filename):
>
>       def _readimage(self):
>           im=Image.open(filename)
>           self._readImage(filename)
>           self._wd,self._ht=im.size
>           for y in range(self._ht):
>                for x in range(self._wd):
>                      r,g,b=im.getpixel((x,y))
>                      pval=self.rgbTodoubleval((r,g,b))
>                      self._pixellist.append(pval)

The PIL docs at [1] say that using getpixel is very slow, and suggest
to use getdata instead. And you want a flat representation anyway,
just like getdata. So replace the for loops above with:

rgbTodoubleval = self.rgbTodoubleval
self._pixellist = [rgbTodoubleval(pix) for pix in
im.getdata()]

I've not tested it, but should be faster.

> def rgbTodoubleval(self,(r,g,b)):
> alpha=255
> pixelvalue=(alpha<<24)|(r<<16 )|( g<<8) | b
> return pixelvalue

I don't get the name - why "rgb to double"? This does not return a
"double", but a long integer, even if you intended to return a 32 bit
integer.
This version returns an integer:

from struct import pack, unpack
def rgbTodoubleval((r,g,b)):
alpha=255
return unpack("l", pack("BBBB", b, g, r, alfa))[0]

It *may*, or not, be what you want...

--
Gabriel Genellina

Gabriel Genellina

12/30/2007 8:02:00 AM

0

On 30 dic, 04:57, Gabriel Genellina <gagsl-...@yahoo.com.ar> wrote:

> The PIL docs at [1] say that using getpixel is very slow, and suggest

Sorry, dropped the reference:

[1] http://www.effbot.org/imagingbook/image.htm#tag-Image.Imag...

--
Gabriel Genellina

jimgardener

12/30/2007 1:54:00 PM

0

> (May I ask why an accessor like getpixellist() instead of simply
> rgbimage1.pixellist?)

sorry,
bad style of coding on my part..was doing java stuff..

>
> I don't get the name - why "rgb to double"? This does not return a
> "double", but a long integer,

actually it was to be of 'long' type not double..sorry again

jim

Gabriel Genellina

12/30/2007 2:08:00 PM

0

On 30 dic, 10:53, jimgarde...@gmail.com wrote:

> > I don't get the name - why "rgb to double"? This does not return a
> > "double", but a long integer,
>
> actually it was to be of 'long' type not double..sorry again

Notice that a Python 'long' is an infinite range integer; the C 'long'
type maps to Python 'int'. Perhaps you want the latter.

--
Gabriel Genellina