Display a BMP Image

Introduction

The BMP file format, or bitmap, is a raster graphics image file format used to store bitmap digital images, independently of the display device. (Wikipedia)

Metadata Definition

Metadata is "data that provides information about other data", but not the content of the data itself. Image file headers are metadata. They are part of an image file that describes the image in the file. For example, width, height, pixel size, etc.

Project #1

In this project you will access the raw bytes of a BMP image file, read the file headers, and write the image pixels into a graphics window.

To see code that can access BMP files as bytes click HERE .

Use the simple graphics library graphics.py. Click HERE for more information.

Note: In real life you would use a better, faster, more efficient graphics library. But, graphics.py is an excellent library for learning.

Project #2

Create an interactive BMP 24-bit pixel row padding calculator. Ask the user to enter the number of pixels. Display the number of pixels and the padding.

Project #3

In this project you will access the raw bytes of a PNG image file and display its header information. (Expand this project by writing the pixels into a graphics window?)

PNG (Wikipedia)

Chapter 8. PNG Basics

PNG (Portable Network Graphics) Specification, Version 1.2

Project #4a

The same as Project #1 except use Python's Pillow module.

Note: The pillow library is the successor to the PIL library. It still uses the PIL package name to serve as a drop-in replacement.
  1. version of PIL installed
    from PIL import Image ##print(dir(Image)) print('PIL',Image.__version__)

  2. install Pillow

  3. import PIL

Project #4b

The same as Project #3 except use Python's Pillow module.

Project #5

The same as Project #3 except use JPG or GIF images.

A suggestion on a program design

  1. read the BMP file into a byte array
  2. get the image width and height from the header
  3. create a graphics window using the width and height
  4. get the start of (the offset to) the image pixels from the header
  5. process one pixel at a time
  6. write each pixel into the graphics window

First create the code to access BMP header and pixels. Then add the graphics.

A suggestion on creating BMP test files

I suggest you create small BPM files using a paint program (like Windows paint). I used a Mona Lisa image, 50x57 pixels, for testing.