Home > Programming & scripting, Python > BMPDecoder in python. BMPDecode version 1.0 released

BMPDecoder in python. BMPDecode version 1.0 released

December 26th, 2006

Just got finished with a release on a small project I’ve been working on to learn how windows bitmaps are constructed. Learned a whole lot creating this module and perhaps it might be useful for someone… Feel free to comment this post with feedback, or simply email me (bjokem-4@student.ltu.se). The module is open source (GPL-license).

Download!
Files only available in archives since GPL requires the license to be bundled.
Download! (updated 1.1)

- BMPDecode version 1.0 -
Features:

Kan decode all commonly used bmp encodings as well as some not so commonly used. Some of the weirder formats such as OS/2 version 1 bitmaps are not included (yet). The decoded object grants easy access to the color of each individual pixel, as well as header-data.

The supported BMP-encodings so far are

  • 1-bit bitmaps
  • 4-bit bitmaps
  • 4-bit rle-4 compressed bitmaps
  • 8-bit bitmaps
  • 4-bit rle-8 compressed bitmaps
  • 16-bit bitmaps (rgb555)
  • 24-bit bitmaps
  • 32-bit bitmaps (rgb888)

Example of using the module

>>> import bmpdecode
>>> decoder = bmpdecode.BMPDecoder("cat.bmp")
>>> decoder.get_height()
410
>>> decoder.get_width()
509
>>> decoder.print_fileheader()
self.bfType:  BM
self.bfSize:  626534
self.bfReserved1:
self.bfReserved2:
self.bfOffBits:  54
>>> decoder.print_fileinfoheader()
self.biSize:  40
self.biWidth:  509
self.biHeight:  410
self.biPlanes:  1
self.biBitCount:  24
self.biCompression:  0
self.biSizeImage:  626480
self.biXPelsPerMeter:  2834
self.biYPelsPerMeter:  2834
self.biClrUsed:  0
self.biClrImportant:  0
>>> decoder.get_pixel_rgb(100,100)
(66, 62, 63)
>>> decoder.get_pixel_rgb(220,107)
(45, 39, 39)
>>> decoder.get_pixel_hexclr(220,107)
'0x2d2727'
>>> decoder.get_pixel_hexclr(10,10)
'0x352d20'

Documentation

Module contains all documentation. Here is the output of help(bmpdecode)

DESCRIPTION
    BMPDecoder:
    A module for decoding BMP-images. Grants easy access to the color of
    individual pixels.

    Revision history:

    v1.0 - 25/12-2006: Bjorn Kempen. First release.
    1,4,8,16,24,32 bit bitmaps supported
    4bit and 8bit rle-compressed bitmaps supported

    Contact information:

    Bjorn Kempen
    bjokem-4@student.ltu.se
    http://www.buffis.com

CLASSES
    BMPDecoder

    class BMPDecoder
     |  Class for decoding BMP-images.
     |  Supported encodings are:
     |  1-bit bitmap
     |  4-bit bitmap
     |  8-bit bitmap
     |  16-bit bitmap (using rgb555 bitmask)
     |  24-bit bitmap
     |  32-bit bitmap (default 32bit only. no bitfield implementation yet)
     |  4-bit rle_4 compressed bitmap
     |  8-bit rle_8 compressed bitmap
     |
     |  Unsupported encodings:
     |  16-bit bitmaps with bitfields
     |  32-bit bitmaps with bitfields
     |  32-bit transparency
     |  OS/2 version 1 bitmap
     |  OS/2 version 2 bitmap
     |
     |  Chances that you will encounter a BMP not suited for decoding using this module
     |  during ordinary use is extremely slim.
     |
     |  Methods defined here:
     |
     |  __init__(self, filename)
     |      BMPDecoder(filename) -> a BMPDecoder object from the
     |      bitmap contained in the file which the filename specify
     |
     |  get_height(self)
     |      getHeight() -> height as an integer
     |      Returns the height of the bitmap.
     |
     |  get_pixel_hexclr(self, x, y)
     |      get_pixel_hexclr(position x, position y) -> color of pixel at
     |      (x,y) as a hexadecimal color (0x######)
     |      Returns the color of a pixel in a bitmap as a hex color
     |
     |  get_pixel_rgb(self, x, y)
     |      get_pixel_rgb(position x, position y) -> color of pixel at
     |      (x,y) as a (r,g,b) tuple
     |      Returns the color of a pixel in a bitmap as a tuple of
     |      its components in red, green and blue
     |
     |  get_width(self)
     |      Returns the width of the bitmap.
     |      getWidth() -> width as an integer
     |
     |  print_fileheader(self)
     |      printFileHeader() -> no return value
     |      Prints the file header of the bitmap.
     |      This header contains information about the file that you most likely wont need
     |
     |  print_fileinfoheader(self)
     |      printFileInfoHeader() -> no return value
     |      Prints the file info header of the bitmap.
     |      This header contains information about the bitmap,
     |      such as width, height, encoding and so on

buffi Programming & scripting, Python

  1. No comments yet.
  1. No trackbacks yet.