Reverse order of bit in repeating seqence of byte string
google at mrabarnett.plus.com
Sat Jan 3 00:00:54 CET 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):
>> blue = blist[start]
>> red = blist[start+2]
>> blist[start] = red
>> blist[start+2] = blue
>> except IndexError:
>> new_buffer = ''.join(blist)
>> new_buffer is then passed to a wx program to create and image.
>> Any thoughts comments would be appreciated.
>> 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
>>> s = "012345678"
>>> from array import array
>>> a = array("b", s)
array('b', [48, 49, 50, 51, 52, 53, 54, 55, 56])
>>> a[0::3], a[2::3] = a[2::3], a[0::3]
array('b', [50, 49, 48, 53, 52, 51, 56, 55, 54])
More information about the Python-list