FAQ
Okay. I'm getting a bit frustrated here. I'm trying to suck
bits out of a file (which is in a simple, but non standard
image format) and then convert this to a bitmap for wxPython.
I haven't gotten to the wxBitmap part yet, which I am assuming
will be easy, but the in-between part is driving me nuts.

First, in one fell swoop of antigenious, I seem to have
forgotten how to convert strings to sequences of char and
vice versa. Eeek. Help? Will someone please remind me and
hit me with a rubber mallet??? :)

Second, this whole paradigm seems so skrewball. While I
know exactly how to implement all of this in a external
C module, the program I'm writing is just a one off, and
I was expecting it to be easier in python.

The bitmap image is 16 bit color, but it's stored in
byte runs. As I load the image, I need to load 2,4,6,
8...44,42,40...2, 16 bit chunks at a time, but as I'm
doing this I have to create "black" runs on either side,
making the whole thing 44x44 (it's an isometric tile...
a square rotated 45 degrees, but bitmaps are square,
get it?)

I'm not seeing an elegant solution here; loading the
image as a string to begin with isn't right, because
my chunks are actually two byte chunks.

Anyone with any practical experience on how to *BEST*
do this kind of thing?

Am I just not thinking about this right?




C/

Search Discussions

  • David Goodger at Jul 23, 2000 at 4:41 am

    on 2000-07-22 23:38, Courageous (jkraska1 at san.rr.com) wrote:
    First, in one fell swoop of antigenious, I seem to have
    forgotten how to convert strings to sequences of char and
    vice versa. Eeek. Help? Will someone please remind me and
    hit me with a rubber mallet??? :)
    s="abcd"
    map(None, s)
    ['a', 'b', 'c', 'd']

    ... is one way. There are probably others. Consider youself whalloped.
    Second, this whole paradigm seems so skrewball. ...
    get it?)
    No, not really. Post an example?

    --
    David Goodger dgoodger at bigfoot.com Open-source projects:
    - The Go Tools Project: http://gotools.sourceforge.net
    (more to come!)
  • Courageous at Jul 23, 2000 at 4:56 am

    vice versa. Eeek. Help? Will someone please remind me and
    hit me with a rubber mallet??? :)
    ... is one way. There are probably others. Consider youself whalloped.
    Yeah, amongst all the various documentation, I finally found my
    way to FAQTS. It was:
    l=list("fred")
    l
    ['f', 'r', 'e', 'd']
    string.join(l,"")
    'fred'
    >>>
    Second, this whole paradigm seems so skrewball. ...
    get it?)
    Um, well suppose I do something like...

    myfile.seek ( 2000, 0 )
    str = myfile.read (20)

    This will result in a sequence of 20 bytes ('char'),
    But these are 16 bit RGB values (1 pad, 5/5/5 R/G/B).
    In C, I would quite possibly load these into ints,
    as 16 bit values straight up, and manipulate them that
    way. Now while I understand that I can unpack into
    16 bit integers, manipulate, and then pack back to
    char all via the struct module, this is seeming like
    a great deal of transmogrification.

    Say for example I want to increase or decrease R,
    G, or B values. Or, say, I want to convert to 32
    bit color bitmaps?



    C/
  • Courageous at Jul 23, 2000 at 5:30 am

    get it?)
    No..
    The image is runs of values where 'X' represents a 16 bit RGB value.
    It's like this:

    XX
    XXXX
    XXXXXX
    ...
  • Darrell Gallion at Jul 23, 2000 at 1:24 pm
    Here's some helpful utils I use when working with binary data.
    They aren't well tested because I use a 'C' version of these same uitls.
    Although I regret doing any of them in 'C'.

    http://www.dorb.com/darrell/utils/hexConvert.py
    Converts binary to hex like so:
    0x3031 0x3233 0x3435
    And back to binary. Nice for debugging, since I hate reading octal.

    http://www.dorb.com/darrell/utils/bitShift.py
    This is unsigned bit shifting and rotate.


    --Darrell

    ----- Original Message -----
    From: "Courageous" <jkraska1 at san.rr.com>
    Newsgroups: comp.lang.python
    To: <python-list at python.org>
    Sent: Sunday, July 23, 2000 1:30 AM
    Subject: Re: bit manipulation frustration

    get it?)
    No..
    The image is runs of values where 'X' represents a 16 bit RGB value.
    It's like this:

    XX
    XXXX
    XXXXXX
    ...
    From 2 to 44 to 2 again.
    However, upon load, this needs to be converted to
    a bitmap like this (where "-" is black, and X
    is a 16 bit RGB value):

    ---------------------XX---------------------
    --------------------XXXX--------------------
    -------------------XXXXXX-------------------
    ------------------XXXXXXXX------------------
    -----------------XXXXXXXXXX-----------------
    ----------------XXXXXXXXXXXX----------------
    ---------------XXXXXXXXXXXXXX---------------
    --------------XXXXXXXXXXXXXXXX--------------
    -------------XXXXXXXXXXXXXXXXXX-------------
    ------------XXXXXXXXXXXXXXXXXXXX------------
    -----------XXXXXXXXXXXXXXXXXXXXXX-----------
    ----------XXXXXXXXXXXXXXXXXXXXXXXX----------
    ---------XXXXXXXXXXXXXXXXXXXXXXXXXX---------
    --------XXXXXXXXXXXXXXXXXXXXXXXXXXXX--------
    -------XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-------
    ------XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX------
    -----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-----
    ----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX----
    ---XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX---
    --XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX--
    -XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    -XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-
    --XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX--
    ---XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX---
    ----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX----
    -----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-----
    ------XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX------
    -------XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-------
    --------XXXXXXXXXXXXXXXXXXXXXXXXXXXX--------
    ---------XXXXXXXXXXXXXXXXXXXXXXXXXX---------
    ----------XXXXXXXXXXXXXXXXXXXXXXXX----------
    -----------XXXXXXXXXXXXXXXXXXXXXX-----------
    ------------XXXXXXXXXXXXXXXXXXXX------------
    -------------XXXXXXXXXXXXXXXXXX-------------
    --------------XXXXXXXXXXXXXXXX--------------
    ---------------XXXXXXXXXXXXXX---------------
    ----------------XXXXXXXXXXXX----------------
    -----------------XXXXXXXXXX-----------------
    ------------------XXXXXXXX------------------
    -------------------XXXXXX-------------------
    --------------------XXXX--------------------
    ---------------------XX---------------------

    This is an isometric tile, where the only data
    stored in the file is the diamond part of X's
    in the middle. The -'s are all predictable, so
    there's no reason to store them.

    Ergo, I'm looking for the best way to create
    a bitmap from something that isn't one.



    C/
    --
    http://www.python.org/mailman/listinfo/python-list
  • Peter Schneider-Kamp at Jul 23, 2000 at 7:57 am

    Courageous wrote:
    Say for example I want to increase or decrease R,
    G, or B values. Or, say, I want to convert to 32
    bit color bitmaps?
    def toRGB(v):
    ... i = (ord(v[1]) << 8) + ord(v[0])
    ... return [i >> 11, (i >> 5) & 63, i & 31]
    ...
    def fromRGB(rgb):
    ... i = (rgb[0] << 11) + (rgb[1] << 5) + rgb[2]
    ... return chr(i & 255)+chr(i >> 8)
    ...

    This converts a two character string into an rgb triple
    and back.

    Hope that helps,
    Peter
    --
    Peter Schneider-Kamp ++47-7388-7331
    Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
    N-7050 Trondheim http://schneider-kamp.de
  • Peter Schneider-Kamp at Jul 23, 2000 at 8:07 am

    Peter Schneider-Kamp wrote:
    Courageous wrote:
    Say for example I want to increase or decrease R,
    A less general, but faster way (untested):

    def incR(v,inc = 1):
    return v[0]+chr(ord(v[1]) + (inc << 3))

    Note that little-endian/big-endian byte ordering must
    be considered.

    Peter
    --
    Peter Schneider-Kamp ++47-7388-7331
    Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
    N-7050 Trondheim http://schneider-kamp.de
  • Peter Schneider-Kamp at Jul 23, 2000 at 8:02 am

    Courageous wrote:
    Say for example I want to increase or decrease R,
    G, or B values. Or, say, I want to convert to 32
    bit color bitmaps?
    Oh yes:
    def from16to32(v):
    ... i = (ord(v[1]) << 8) + ord(v[0])
    ... return chr(i >> 11)+chr((i >> 5) & 63)+chr(i & 31)+'\000'
    ...

    Peter
    --
    Peter Schneider-Kamp ++47-7388-7331
    Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
    N-7050 Trondheim http://schneider-kamp.de
  • Mike Fletcher at Jul 23, 2000 at 4:59 am
    (This is untested (don't have data file), and hacky, but should give ideas
    for a one-off):

    input = open('somefile.dat').read()
    file = []
    currentlength = 4
    while input:
    pad = '\000'*( (88-currentlength)/2)
    line = pad + input[:currentlength] + pad
    input = input[currentlength:]
    if len(file) > 21:
    currenlength = currentlength - 4
    else:
    currenlength = currentlength + 4

    Oh, for converting to 16-bit integers (unsigned)
    import struct
    def convert( line ):
    return struct.unpack ( 'H'*(len(line)/2), line )

    (That's in machine-native order, use '>'+('H'*...) for network order).

    HTH,
    Mike

    -----Original Message-----
    From: Courageous [mailto:jkraska1 at san.rr.com]
    Sent: Saturday, July 22, 2000 11:39 PM
    To: python-list at python.org
    Subject: bit manipulation frustration



    Okay. I'm getting a bit frustrated here. I'm trying to suck
    bits out of a file (which is in a simple, but non standard
    image format) and then convert this to a bitmap for wxPython.
    I haven't gotten to the wxBitmap part yet, which I am assuming
    will be easy, but the in-between part is driving me nuts.

    First, in one fell swoop of antigenious, I seem to have
    forgotten how to convert strings to sequences of char and
    vice versa. Eeek. Help? Will someone please remind me and
    hit me with a rubber mallet??? :)

    Second, this whole paradigm seems so skrewball. While I
    know exactly how to implement all of this in a external
    C module, the program I'm writing is just a one off, and
    I was expecting it to be easier in python.

    The bitmap image is 16 bit color, but it's stored in
    byte runs. As I load the image, I need to load 2,4,6,
    8...44,42,40...2, 16 bit chunks at a time, but as I'm
    doing this I have to create "black" runs on either side,
    making the whole thing 44x44 (it's an isometric tile...
    a square rotated 45 degrees, but bitmaps are square,
    get it?)

    I'm not seeing an elegant solution here; loading the
    image as a string to begin with isn't right, because
    my chunks are actually two byte chunks.

    Anyone with any practical experience on how to *BEST*
    do this kind of thing?

    Am I just not thinking about this right?




    C/
  • Mike Fletcher at Jul 23, 2000 at 5:00 am
    list( string) is one of the other ways :) .

    Enjoy,
    Mike

    -----Original Message-----
    From: David Goodger [mailto:dgoodger at bigfoot.com]
    Sent: Sunday, July 23, 2000 12:41 AM
    To: python-list at python.org
    Cc: jkraska1 at san.rr.com
    Subject: Re: bit manipulation frustration

    ...
    s="abcd"
    map(None, s)
    ['a', 'b', 'c', 'd']
    ...
  • Mike Fletcher at Jul 23, 2000 at 5:20 am
    Convoluted, yes, but this might give you some ideas for manipulations (or it
    might give you convulsions ;0) )...

    import struct
    def toRGB( line ):
    line = struct.unpack( 'H'*len(line), line)
    result = []
    R = 64512
    G = 2016
    B = 63
    for integer in line:
    result.append( (
    R&integer >> 10,
    G&integer >> 5,
    B&integer,
    ) )
    return result

    def toString( line ):
    result = []
    for r,g,b in line:
    result.append(
    r << 10 + g << 5 + b
    )
    return apply( struct.pack, ( 'H'*len(result),)+tuple(result) )

    Oh, in case you're wondering, this will be _ridiculously_ slow, Python's not
    good at bit-fiddling :) . That's one reason PIL's C extensions exist :) .
    You'll probably find something that can read 16-bit integers into RGB values
    somewhere in the codebase.

    HTH,
    Mike

    -----Original Message-----
    From: Courageous [mailto:jkraska1 at san.rr.com]
    Sent: Sunday, July 23, 2000 12:56 AM
    To: python-list at python.org
    Subject: Re: bit manipulation frustration


    vice versa. Eeek. Help? Will someone please remind me and
    hit me with a rubber mallet??? :)
    ... is one way. There are probably others. Consider youself whalloped.
    Yeah, amongst all the various documentation, I finally found my
    way to FAQTS. It was:
    l=list("fred")
    l
    ['f', 'r', 'e', 'd']
    string.join(l,"")
    'fred'
    >>>
    Second, this whole paradigm seems so skrewball. ...
    get it?)
    Um, well suppose I do something like...

    myfile.seek ( 2000, 0 )
    str = myfile.read (20)

    This will result in a sequence of 20 bytes ('char'),
    But these are 16 bit RGB values (1 pad, 5/5/5 R/G/B).
    In C, I would quite possibly load these into ints,
    as 16 bit values straight up, and manipulate them that
    way. Now while I understand that I can unpack into
    16 bit integers, manipulate, and then pack back to
    char all via the struct module, this is seeming like
    a great deal of transmogrification.

    Say for example I want to increase or decrease R,
    G, or B values. Or, say, I want to convert to 32
    bit color bitmaps?



    C/
  • Peter Schneider-Kamp at Jul 23, 2000 at 7:38 am

    Courageous wrote:
    First, in one fell swoop of antigenious, I seem to have
    forgotten how to convert strings to sequences of char and
    vice versa. Eeek. Help? Will someone please remind me and
    hit me with a rubber mallet??? :)
    map(None, "hello courageous")
    ['h', 'e', 'l', 'l', 'o', ' ', 'c', 'o', 'u', 'r', 'a', 'g', 'e', 'o',
    'u', 's']
    >>>

    But what do you need this for? You can work on a string
    like on that list except that the list is mutable.

    I am not sure if I have understood your other problem though.
    Maybe the following class can help you?

    from operator import add

    class isotile:
    def __init__(self, name, size, inc = 2):
    self.up = 1
    self.pos = 0
    self.num = inc
    self.size = size
    self.inc = inc
    self.data = open(name, "rb").read()
    def readline(self):
    if not self.num:
    return ''
    newpos = self.pos + self.num
    pad = (self.size - self.num) / 2
    line = self.data[self.pos:newpos]
    self.pos = newpos
    if self.up:
    if self.num == self.size:
    self.up = 0
    self.num = self.num - self.inc
    else:
    self.num = self.num + self.inc
    else:
    self.num = self.num - self.inc
    return pad * '\000' + line + pad * '\000'
    def readlines(self):
    result = [self.readline()]
    while result[-1]:
    result.append(self.readline())
    return result[:-1]
    def read(self):
    return reduce(add, self.readlines())

    if __name__ == "__main__":
    open("isotile_test.dat","wb").write('112222333333444455')
    a = isotile("isotile_test.dat", 6)
    print a.readlines()
    --
    Peter Schneider-Kamp ++47-7388-7331
    Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
    N-7050 Trondheim http://schneider-kamp.de

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 23, '00 at 3:38a
activeJul 23, '00 at 1:24p
posts12
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase