Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reverse order of bit in repeating seqence of byte string

Reply
Thread Tools

Reverse order of bit in repeating seqence of byte string

 
 
imageguy
Guest
Posts: n/a
 
      01-02-2009
I am looking for the most efficient method of replacing a repeating
sequence in a byte string returned from a imaging .dll, connected via

I receive the byte string with the following sequence 'bgrbgrbgrbgr'
and I would like to convert this to 'rbgrbgrbgrbg'
FWIW, the string is created using ctypes.create_string_buffer function

The following code works but feels a bit clunk and is rather slow too.

blist = list(buffer)
for start in xrange(0,len(blist), 3):
try:
blue = blist[start]
red = blist[start+2]
blist[start] = red
blist[start+2] = blue
except IndexError:
pass

new_buffer = ''.join(blist)

new_buffer is then passed to a wx program to create and image.

Any thoughts comments would be appreciated.

geoff.

PS: I started this post earlier, but I think I hit the send button
too soon. My apologies to the group for sloppy typing.

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      01-02-2009
imageguy wrote:
> I am looking for the most efficient method of replacing a repeating
> sequence in a byte string returned from a imaging .dll, connected via
>
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'
> FWIW, the string is created using ctypes.create_string_buffer function
>
> The following code works but feels a bit clunk and is rather slow too.
>
> blist = list(buffer)
> for start in xrange(0,len(blist), 3):
> try:
> blue = blist[start]
> red = blist[start+2]
> blist[start] = red
> blist[start+2] = blue
> except IndexError:
> pass
>
> new_buffer = ''.join(blist)
>
> new_buffer is then passed to a wx program to create and image.
>
> Any thoughts comments would be appreciated.
>
> geoff.
>
> PS: I started this post earlier, but I think I hit the send button
> too soon. My apologies to the group for sloppy typing.
>

May not be any quicker, but it works:

blues = buffer[0::3]
greens = buffer[1::3]
reds = buffer[2::3]

result = "".join("".join(x) for x in zip(reds, blues, greens))

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
 
 
 
Francesco Bochicchio
Guest
Posts: n/a
 
      01-02-2009
imageguy ha scritto:
> I am looking for the most efficient method of replacing a repeating
> sequence in a byte string returned from a imaging .dll, connected via
>
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'
> FWIW, the string is created using ctypes.create_string_buffer function
>
> The following code works but feels a bit clunk and is rather slow too.
>
> blist = list(buffer)
> for start in xrange(0,len(blist), 3):
> try:
> blue = blist[start]
> red = blist[start+2]
> blist[start] = red
> blist[start+2] = blue
> except IndexError:
> pass
>
> new_buffer = ''.join(blist)
>
> new_buffer is then passed to a wx program to create and image.
>
> Any thoughts comments would be appreciated.
>
> geoff.
>
> PS: I started this post earlier, but I think I hit the send button
> too soon. My apologies to the group for sloppy typing.
>

You could try the same algorithm on an array.array object : it might be
faster.

Ciao
----
FB
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-02-2009
imageguy wrote:
> I am looking for the most efficient method of replacing a repeating
> sequence in a byte string returned from a imaging .dll, connected via
>
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'


For speed, I would look at PIL or pygame for existing function, or use
numpy -- read into 2-d array, swap first and third members, flatten.

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-02-2009
Francesco Bochicchio wrote:
> imageguy ha scritto:
>> I am looking for the most efficient method of replacing a repeating
>> sequence in a byte string returned from a imaging .dll, connected via
>>
>> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
>> and I would like to convert this to 'rbgrbgrbgrbg'
>> FWIW, the string is created using ctypes.create_string_buffer function
>>
>> The following code works but feels a bit clunk and is rather slow too.
>>
>> blist = list(buffer)
>> for start in xrange(0,len(blist), 3):
>> try:
>> blue = blist[start]
>> red = blist[start+2]
>> blist[start] = red
>> blist[start+2] = blue
>> except IndexError:
>> pass
>>
>> new_buffer = ''.join(blist)
>>
>> new_buffer is then passed to a wx program to create and image.
>>
>> Any thoughts comments would be appreciated.
>>
>> geoff.
>>
>> PS: I started this post earlier, but I think I hit the send button
>> too soon. My apologies to the group for sloppy typing.
>>

> You could try the same algorithm on an array.array object : it might be
> faster.
>
>>> s = "012345678"
>>> from array import array
>>> a = array("b", s)
>>> a

array('b', [48, 49, 50, 51, 52, 53, 54, 55, 56])
>>> a[0::3], a[2::3] = a[2::3], a[0::3]
>>> a

array('b', [50, 49, 48, 53, 52, 51, 56, 55, 54])
>>> a.tostring()

'210543876'

 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      01-02-2009
imageguy:
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'
> FWIW, the string is created using ctypes.create_string_buffer function


MRAB:
> *>>> a.tostring()
> '210543876'


That's not the required 'rbgrbgrbgrbg', but you are close to a correct
solution:

>>> from array import array
>>> s = 'bgrbgrbgrbgr'
>>> a = array("B", s) # uppercase B
>>> a[0::3], a[1::3], a[2::3] = a[2::3], a[0::3], a[1::3]
>>> a.tostring()

'rbgrbgrbgrbg'

Bye,
bearophile
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-03-2009
On Jan 3, 5:34*am, imageguy <imageguy1...@gmail.com> wrote:
> I am looking for the most efficient method of replacing a repeating
> sequence in a byte string returned from a imaging .dll, connected via
>
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'
> FWIW, the string is created using ctypes.create_string_buffer function
>
> The following code works but feels a bit clunk and is rather slow too.


For some very strange definition of "works". You say you have 'bgr'
and want to convert it to 'rbg'. The following code converts 'bgr' to
'rgb', which is somewhat more plausible, but not what you said you
wanted.

>
> blist = list(buffer)
> for start in xrange(0,len(blist), 3):
> * *try:
> * * * * blue = blist[start]
> * * * * red = blist[start+2]
> * * * * blist[start] = red
> * * * * blist[start+2] = blue
> * *except IndexError:
> * * * *pass
>
> new_buffer = ''.join(blist)
>


 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      01-03-2009
imageguy wrote:
> I am looking for the most efficient method of replacing a repeating
> sequence in a byte string returned from a imaging .dll, connected via
>
> I receive the byte string with the following sequence 'bgrbgrbgrbgr'
> and I would like to convert this to 'rbgrbgrbgrbg'
> FWIW, the string is created using ctypes.create_string_buffer function
>
> The following code works but feels a bit clunk and is rather slow too.
>
> blist = list(buffer)
> for start in xrange(0,len(blist), 3):
> try:
> blue = blist[start]
> red = blist[start+2]
> blist[start] = red
> blist[start+2] = blue
> except IndexError:
> pass
>
> new_buffer = ''.join(blist)
>
> new_buffer is then passed to a wx program to create and image.
>
> Any thoughts comments would be appreciated.
>
> geoff.
>
> PS: I started this post earlier, but I think I hit the send button
> too soon. My apologies to the group for sloppy typing.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I've not seen anyone mention numpy yet, but
numpy has a nice (efficient) way to do this:

>>> import numpy
>>> s = 'bgrBGRcbaCBA'
>>> a=numpy.array(s, 'c') # Initialize an array
>>> a.shape = (4,3) # Reinterpret as a ?? by 3 array
>>> b=a[...,::-1] # reverse the second dimension
>>> print b.tostring() # Convert back to a string.

rgbRGBabcABC


Gary Herron





 
Reply With Quote
 
imageguy
Guest
Posts: n/a
 
      01-03-2009
On Jan 2, 7:33*pm, John Machin <sjmac...@lexicon.net> wrote:

> For some very strange definition of "works". You say you have 'bgr'
> and want to convert it to 'rbg'. The following code converts 'bgr' to
> 'rgb', which is somewhat more plausible, but not what you said you
> wanted.


Well that's embarrassing ... you are correct. I need to convert from
'bgr' to 'rgb'

Thanks to all others for suggestions

FWIW, I realized the the C.types string buffer is/was mutable so
settled on this;

for start in xrange(0, ctypes.sizeof(buffer), 3):
if buffer[start] != buffer[start+2]:
#only need to swap the bits if they are different. ie if
both are white or black, no change is required.
blue, red = buffer[start], buffer[start+2]
buffer[start], buffer[start+2] = red, blue

This was about 30-40% faster that converting to list, processing and
then converting back again.

Will try the array module too
.... but I think I need to find a new graphic package rather that makes
working with the bits easier.

g.

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-03-2009
On Jan 4, 7:10*am, imageguy <imageguy1...@gmail.com> wrote:
> On Jan 2, 7:33*pm, John Machin <sjmac...@lexicon.net> wrote:
>
> > For some very strange definition of "works". You say you have 'bgr'
> > and want to convert it to 'rbg'. The following code converts 'bgr' to
> > 'rgb', which is somewhat more plausible, but not what you said you
> > wanted.

>
> Well that's embarrassing ... you are correct. *I need to convert from
> 'bgr' to 'rgb'
>
> Thanks to all others for suggestions
>
> FWIW, I realized the the C.types string buffer is/was mutable so
> settled on this;
>
> for start in xrange(0, ctypes.sizeof(buffer), 3):
> * * if buffer[start] != buffer[start+2]:
> * * * * *#only need to swap the bits if they are different. *ie if
> both are white or black, no change is required.
> * * * * *blue, red = buffer[start], buffer[start+2]
> * * * * *buffer[start], buffer[start+2] = red, blue


You are likely find that using
buffer[start], buffer[start+2] = buffer[start+2], buffer[start]
is faster.

Cheers,
John

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SLV reverse bit order Luis Cupido VHDL 8 11-02-2012 04:05 PM
Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 benn686@hotmail.com C++ 9 08-22-2007 12:13 AM
reverse bit order mike7411@gmail.com C++ 20 10-10-2006 09:40 PM
Reading a signed byte in network byte order Robert Evans Ruby 7 11-15-2005 11:14 PM
Escape Seqence Not Working in mailto jpan Javascript 3 10-16-2004 07:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57