python - How would I implement a bit map? -


i wish implement 2d bit map class in python. class have following requirements:

  1. allow creating of arbitrarily sized 2d bitmaps. i.e. create 8 x 8 bitmap (8 bytes), like:

    bitmap = bitmap(8,8) 
  2. provide api access bits in 2d map boolean or integer values, i.e.:

    if bitmap[1, 2] or bitmap.get(0, 1) 
  3. able retrieve data packed binary data. each row of bit map concatenated , returned binary data. may padded nearest byte or similar.

    bitmap.data() 
  4. be able create new maps binary data retrieved:

    new_bitmap = bitmap(8, 8, bitmap.data()) 

i know python able perform binary operations, i'd suggestions how best use them implement class.

bit-packing numpy ( scipy ) arrays looking for. example shows 4x3 bit (boolean) array packed 4 8-bit bytes. unpackbits unpacks uint8 arrays boolean output array can use in computations.

>>> = np.array([[[1,0,1], ...                [0,1,0]], ...               [[1,1,0], ...                [0,0,1]]]) >>> b = np.packbits(a,axis=-1) >>> b array([[[160],[64]],[[192],[32]]], dtype=uint8) 

if need 1-bit pixel images, pil place look.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -