473,799 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",pix elComponent) << 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*w idth) <<<<<<<<< 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?o cid=TXT_TAGLM_W L_Refresh_mobil e_052008>

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

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
2 1651
On May 16, 2:11 am, Gary Herron <gher...@island training.comwro te:
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",pix elComponent) << 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...@island training.comwro te:
>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",pix elComponent) << 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5458
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are coming through fine when using (integer1,) = struct.unpack('l', line) except when line contains "carriage-return" "linefeed" which are valid binary packed values. Error = unpack string size dows not match format. It seems that
3
3845
by: Andrew Robert | last post by:
Hey everyone, Maybe you can see something I don't. I need to convert a working piece of perl code to python. The perl code is: sub ParseTrig {
4
2804
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do something tdc = struct.unpack('=LiLiLiLi',self.lmf.read(32)) # do something
1
1775
by: santrooper | last post by:
Hello all, I am trying to unpack big wav file (more than 10 mb), because i want to generate Visualization of an wav file, for smaller files (less than 2 mb) it works fine, but for bigger files it gives Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 35 bytes) in /var/www/phptest/mp3processor/thescript.php on line 64 I am pesting the code below PLEASE HELP ==================== <? class wavVisualization{...
0
180
by: Gary Herron | last post by:
Marlin Rowley wrote: You don't need the newsgroup to answer this kind of question. Just try it! abcdefghi abcd Notice that the index does not change the original array. Gary Herron
0
1730
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
17
2320
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='".$file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='".$file_name."&order_by=location'>Location</a></th> <th><a href='".$file_name."&order_by=sighting_date_asc'>Sighting Date</a></th> <th><a href='".$file_name."&order_by=classification'>Code</a></th>...
19
1878
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a href='". $file_name."&order_by=carnumber'>Carnumber</a></th> <th><a href='". $file_name."&order_by=location'>Location</a></th> <th><a href='". $file_name."&order_by=sighting_date_asc'>Sighting
1
2477
by: DJJohnnyG | last post by:
Hi, I am trying to recreate a perl script in C# but have a problem creating a checksum value that a target system needs. In Perl this checksum is calculated using the unpack function: while () { $checksum += unpack("%32C*", $_); } $checksum %= 32767;
0
9686
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
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
3
2938
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.