473,569 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

do i need to create new rgbimage class

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.Fro m my application i
should be able to call rgbimage1.getpi xellist() 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,f ilename):

def _readimage(self ):
im=Image.open(f ilename)
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.getpix el((x,y))
pval=self.rgbTo doubleval((r,g, b))
self._pixellist .append(pval)

def rgbTodoubleval( self,(r,g,b)):
alpha=255
pixelvalue=(alp ha<<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 imagefilenameli st:
myimage=myrgbim age(z)
imagelist.appen d(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
Dec 29 '07 #1
4 1196
On 29 dic, 12:42, jimgarde...@gma il.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.Fro m my application i
should be able to call rgbimage1.getpi xellist() to retrieve the double
values of an image.
(May I ask why an accessor like getpixellist() instead of simply
rgbimage1.pixel list?)
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(f ilename)
* * * * * 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.getpi xel((x,y))
* * * * * * * * * * *pval=self.rgbT odoubleval((r,g ,b))
* * * * * * * * * * *self._pixellis t.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.rgbTodoubl eval
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=(alp ha<<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
Dec 30 '07 #2
On 30 dic, 04:57, Gabriel Genellina <gagsl-...@yahoo.com.a rwrote:
The PIL docs at [1] say that using getpixel is very slow, and suggest
Sorry, dropped the reference:

[1] http://www.effbot.org/imagingbook/im...Image.getpixel

--
Gabriel Genellina
Dec 30 '07 #3
(May I ask why an accessor like getpixellist() instead of simply
rgbimage1.pixel list?)
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
Dec 30 '07 #4
On 30 dic, 10:53, jimgarde...@gma il.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
Dec 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3758
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic properties. Something similar to a java hash table. One can add new properties at runtime and remove them as well. I have a really fancy third...
5
2206
by: Carl Bevil | last post by:
I'm creating a library for internal use that relies on third-party code. I don't want clients of this library to know anything about the third party code, when compiling or running. Generally I've been achieving this by having an abstract base class which defines an interface, and inheriting a concrete class which defines the...
1
1299
by: Julia | last post by:
Hi I have a domain model and I am looking for the correct design patterns to use, The following is my domain model Server-> the is the thread boundaries,the server create a thread the Thread create the Manager ,the Manager create the MessagingService and
2
1770
by: Bonj | last post by:
Hello Can anyone assist with the following class hierarcy problem? I have a series of window classes, the object model currently being as such: Window / | \ / | \ MDIClientWindow | TreeViewWindow WndProcWindow / \
9
2064
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
48
3204
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120...
15
2154
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this...
10
2094
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes #these are the operations + = , - = , * = , 1/ = only if 0 not in .
1
2078
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.