Reverse order of bit in repeating seqence of byte string

John Machin sjmachin at lexicon.net
Sat Jan 3 17:08:21 EST 2009


On Jan 4, 7:10 am, imageguy <imageguy1... at gmail.com> wrote:
> On Jan 2, 7:33 pm, John Machin <sjmac... at 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




More information about the Python-list mailing list