473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

jpeg image read class

hi
i am new to python and PIL and was trying to write a class to read and
write RGB color model jpeg images..
i came up with this class below..i want to know if this is the way to
read/write the RGB color model jpeg..if anyone can give feedback (esp
on the methods writeImage1( ) and writeImage2( ) it wd help my
studies ..
TIA
dn

#jpgpy.py

import Image
from sys import argv
from numpy import array
class JPGFile:
def __init__(self,filename):
self._filename=filename
self._height=0
self._width=0
self._mode=""
self._rgblist=[]
self._pixellist=[]
self._pixarray=None
self.readImage()

def getheight(self):
return self._height
def getwidth(self):
return self._width

def getpixellist(self):
return self._pixellist

def getrgblist(self):
return self._rgblist

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

def readImage(self):
im=Image.open(self._filename)
self._mode=im.mode
self._width,self._height=im.size
for y in range(self._height):
for x in range(self._width):
r,g,b=im.getpixel((x,y))
self._rgblist.append((r,g,b))
pval=self.makepixval((r,g,b))
self._pixellist.append(pval)
self._pixarray=array(self._pixellist,float)

def makergb(self,pixval):
alpha=pixval>>24
red=(pixval>>16 )&255
green=(pixval>>8 )&255
blue=(pixval)& 255
return red,green,blue

def writeImage1(self,fname,data,width,height):
imnew=Image.new(self._mode,(width,height))
newrgblist=[]
for i in range(len(data)):
r,g,b=self.makergb(data[i])
newrgblist.append((r,g,b))
index=0
for y in range(height):
for x in range(width):
imnew.putpixel((x,y),newrgblist[index])
index+=1
imnew.save(fname)

def writeImage2(self,fname,data,width,height):
imnew=Image.new(self._mode,(width,height))
newrgblist=[]
for i in range(len(data)):
r,g,b=self.makergb(data[i])
newrgblist.append((r,g,b))
imnew.putdata(newrgblist)
imnew.save(fname)
imgname=argv[1] #Usage: python jpgpy.py myimage.jpg
print "to read from:",imgname
x=JPGFile(imgname)
print x.getheight(),"",x.getwidth()
pixlist=x.getpixellist()
rgbtpllist=x.getrgblist()
w=x.getwidth()
h=x.getheight()
x.writeImage1("newimage1.jpg",pixlist,w,h)

Oct 31 '07 #1
2 2226
On Wed, 31 Oct 2007 06:48:18 +0000, de****@gmail.com wrote:
i am new to python and PIL and was trying to write a class to read and
write RGB color model jpeg images..
PIL already offers such a class.
i came up with this class below..i want to know if this is the way to
read/write the RGB color model jpeg..if anyone can give feedback (esp
on the methods writeImage1( ) and writeImage2( ) it wd help my
studies ..
Those are bad names. What does 1 and 2 mean in them!?
class JPGFile:
def __init__(self,filename):
self._filename=filename
self._height=0
self._width=0
self._mode=""
This will always be 'RGB' so why store it?
self._rgblist=[]
self._pixellist=[]
self._pixarray=None
In the end you store the image *three times* in different representations.
Is that really necessary?
self.readImage()
Is `readImage()` supposed to be called again later? If not you might add
an underscore in front and give the file name as argument. Then you could
drop `self._filename`.
def getheight(self):
return self._height
def getwidth(self):
return self._width

def getpixellist(self):
return self._pixellist

def getrgblist(self):
return self._rgblist
Maybe get rid of those trivial getters and the underscore in front of the
names. That's a little "unpythonic". Do a web search for "Python is not
Java".
def makepixval(self,(r,g,b)):
alpha=255
pixval=(alpha<<24)|(r<<16 )|( g<<8) | b
return pixval
Looks like an ordinary function to me. `self` is not used.
def readImage(self):
im=Image.open(self._filename)
self._mode=im.mode
self._width,self._height=im.size
for y in range(self._height):
for x in range(self._width):
r,g,b=im.getpixel((x,y))
self._rgblist.append((r,g,b))
pval=self.makepixval((r,g,b))
self._pixellist.append(pval)
self._pixarray=array(self._pixellist,float)
Iterating over `im.getdata()` should be simpler and faster than the nested
loops.
def makergb(self,pixval):
alpha=pixval>>24
red=(pixval>>16 )&255
green=(pixval>>8 )&255
blue=(pixval)& 255
return red,green,blue
Again a simple function.
def writeImage1(self,fname,data,width,height):
imnew=Image.new(self._mode,(width,height))
newrgblist=[]
for i in range(len(data)):
r,g,b=self.makergb(data[i])
newrgblist.append((r,g,b))
If you have ``for i in range(len(something)):`` you almost always really
wanted to iterate over `something` directly. That loop can be written as:

for pixval in data:
rgb = pixval2rgb(pixval)
rgbs.append(rgb)

Or even shorter in one line:

rgbs = map(pixval2rgb, data)

If the `numpy` array is what you want/need it might be more efficient to do
the RGB to "pixval" conversion with `numpy`. It should be faster to shift
and "or" a whole array instead of every pixel one by one in pure Python.

Ciao,
Marc 'BlackJack' Rintsch
Oct 31 '07 #2
Marc
thanx for the reply..it wd help me to write better code..i was trying
to learn PIL and python and may be my java background made it
unpythonic..will try to improve it
again many thanx
dn

Nov 1 '07 #3

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

Similar topics

2
by: Suzanne | last post by:
Hi all, I'm attempting to save images (that are in jpeg format) to a database column of type image. I've read over the Microsoft Knowledge Base articles for this and I can get things working...
3
by: bull.enteract | last post by:
Ok, I start off with a bitmap image. I encode it as a jpeg and send it across the network and pick it up on the other end. Now I have the jpeg image on the other end as an arrray of bytes and I...
16
by: David Lauberts | last post by:
Hi Wonder if someone has some words of wisdom. I have a access 2002 form that contains 2 graph objects that overlay each other and would like to export them as a JPEG to use in a presentation....
3
by: 246C57AE-40DD-4d6b-9E8D-B0F5757BB2A8 | last post by:
Hi. Why can't I read TIFFs compressed by JPEG using GDI+ Image class? I always get out of memory exception. Thanx.
5
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the...
1
by: Smokey Grindel | last post by:
I have a bitmap object I want to return as a JPEG image with a compression set at 90% and progressive passes enabled, how can I do this in .NET 2.0? Progressive passes are not necessary but the...
4
by: Greg | last post by:
I've searched everywhere for a solution Microsoft .NET Framework V 2.0.50727 AutoEventWireup="false" image2.aspx resize an image on the fly but Page_Load is triggered twice: Any suggestion?...
4
by: Pacino | last post by:
Hi, everyone, I am wondering whether it's possible to read part (e.g. 1000*1000) of a huge jpeg file (e.g. 30000*30000) and save it to another jpeg file by pure python. I failed to read the...
2
by: mndprasad | last post by:
hi all i am doing a project in java where i need to convert 10 jpeg images into a single tiff image..conversion of single jpeg image to single tiff is happening but embedding all the 10 jpeg images...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.