470,612 Members | 2,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

Re: How do I use the unpack function?

Marlin Rowley wrote:
All:

I've got a script that runs really slow because I'm reading from a
stream a byte at a time:

// TERRIBLE
for y in range( height ):
for color in range(4):
for x in range( width ):
pixelComponent = fileIO.read(4)
buffer = unpack("!f",pixelComponent) << unpacks ONE
float, but we now can do something with that pixel component.
I can speed this up dramatically if I did this:

// MUCH FASTER
for y in range( height ):
for color in range(4):
pixelComponent = fileIO.read(4*width) <<<<<<<<< GET a LOT more
data from the stream into memory FIRST!!
for x in range( width ):
buffer = unpack( ?????? ) <<<< how do I get each float from
the pixelComponent???
Just carve of the first four bytes of pixelComponent on each pass
through the loop, and unpack that.

for x in range(width):
fourbytes = pixelComponent[:4] # first 4 bytes
pixelComponent = pixelComponent[4:] # All but first four bytes
buffer = unpack("!f", fourbytes)
There are probably better ways overall, but this directly answers your
question.

Gary Herron

>

-M
------------------------------------------------------------------------
With Windows Live for mobile, your contacts travel with you. Connect
on the go.
<http://www.windowslive.com/mobile/overview.html?ocid=TXT_TAGLM_WL_Refresh_mobile_052 008>

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
2 1485
On May 16, 2:11 am, Gary Herron <gher...@islandtraining.comwrote:
Marlin Rowley wrote:
All:
I've got a script that runs really slow because I'm reading from a
stream a byte at a time:
// TERRIBLE
for y in range( height ):
for color in range(4):
for x in range( width ):
pixelComponent = fileIO.read(4)
buffer = unpack("!f",pixelComponent) << unpacks ONE
[snip]
Perhaps the OP might be able to use the Python Imaging Library (PIL)
instead of reinventing an image-file handler.
Jun 27 '08 #2
John Machin wrote:
On May 16, 2:11 am, Gary Herron <gher...@islandtraining.comwrote:
>Marlin Rowley wrote:
>>All:

I've got a script that runs really slow because I'm reading from a
stream a byte at a time:

// TERRIBLE
for y in range( height ):
for color in range(4):
for x in range( width ):
pixelComponent = fileIO.read(4)
buffer = unpack("!f",pixelComponent) << unpacks ONE

[snip]
Perhaps the OP might be able to use the Python Imaging Library (PIL)
instead of reinventing an image-file handler.
Indeed. That's why my original answer included the line:
There are probably better ways overall, but this directly answers
your question.

Other possibilities.

I don't recognize the file format being read in here, but if it is a
standard image format, the PIL suggestion is a good way to go.

Or, if it is an image of not too enormous size, read the *whole* thing
in at once.

Or rather than manipulate the array by carving off 4 bytes at a time,
just index through it in 4 byte chunks:
for i in range(width):
buffer = unpack("!f", pixelComponent[4*i:4*i+4])

Or
for i in range(0,4*width,4):
buffer = unpack("!f", pixelComponent[i:i+4])

Or
Use numpy. Create an array of floats, and initialize it with the byte
string, making sure to take endianess int account. (I'm quite sure this
could be made to work, and then the whole operation is enormously fast
with only several lines of Python code and *no* Python loops.

Or
...?

Gary Herron
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by grant | last post: by
3 posts views Thread by Andrew Robert | last post: by
4 posts views Thread by OhKyu Yoon | last post: by
reply views Thread by Gary Herron | last post: by
reply views Thread by Gary Herron | last post: by
17 posts views Thread by JRough | last post: by
19 posts views Thread by JRough | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.