How do I use the unpack function?

Gary Herron gherron at islandtraining.com
Thu May 15 12:51:22 EDT 2008


Marlin Rowley wrote:
> Thanks for the advice!
>  
> However, I assumed that:
>  
> fourbytes = pixelComponent[:4]
>  
> would shave that byte off the array in pixelComponent.  So that the 
> array is smaller by one on the next iteration.  Is this not the case?
You don't need the newsgroup to answer this kind of question.  Just try it!

 >>> a = 'abcdefghi'
 >>> b = a[:4]
 >>> print a,b
abcdefghi abcd

Notice that the [:4] index does not change the original array.

Gary Herron



>  
> I'm getting weird results now..
>  
> -M
>
>
>
>
> ------------------------------------------------------------------------
>
> > Date: Thu, 15 May 2008 09:11:23 -0700
> > From: gherron at islandtraining.com
> > To: marlin_rowley at hotmail.com
> > CC: python-list at python.org
> > Subject: 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_052008> 
>
> > >
> > > 
> ------------------------------------------------------------------------
> > >
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> ------------------------------------------------------------------------
> Get Free (PRODUCT) RED™ Emoticons, Winks and Display Pics. Check it 
> out! 
> <http://joinred.spaces.live.com?ocid=TXT_HMTG_prodredemoticons_052008>
> ------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list