A bit map picture is exactly what the name makes one suspect: a sequence of bits (0 or 1) that together represent a digital photo. The picture consists of a matrix (rectangle grid) of individual picture elements (or pixels) that each have their own colour. Lets us look at a typical bit map picture to explain this principle:

bitmap

On the left you can see a photo and on the right you can see a 250% enlargement of one of the mountains. As you can see in the enlargement, a bit map picture consists of hundreds of rows and columns of small elements that each have their own colour. One of those elements is called a pixel (which is the short form of picture element). The human eye is not capable of seeing an individual pixel, which is the reason why we cannot see a picture with flowing gradations. The number of pixels that is necessary to get a realistic looking picture, depends largely on the way the picture is going to be used.

The colour of a pixel in a bit map picture can be presented in different ways. For this assignment, we distinguish two categories:

Assignment

We represent a bit map as a list of lists, where the inner lists always represent the colours of a sequence of pixels from a row of the picture. Every colour is represented by an integer or a floating point (black-and-whit pictures) or a list of three integers or floats (colour pictures). In this assignment, we ask you to do image manipulations, by converting every colour of the bit map to another colour. You work as follows: 

Preparation

To determine whether a given object has a certain type of specifications, you can of course use the built-in function type(o), that prints the type of specification of the object o. However, it is better to use the built-in function isinstance(o, t). This function prints a Boolean value that indicates whether the object o belongs to type t, or not.

>>> type(3) == int
True
>>> isinstance(3.14, int)
False
>>> isinstance(3.14, float)
True
>>> isinstance([1, 2, 3], list)
True

Example

>>> color2gray([238, 172, 98])
0.7188156862745098
>>> color2gray([0.7, 0.2, 0.6])
0.3951

>>> invert(168)
87
>>> invert(0.4)
0.6
>>> invert([238, 172, 98])
[17, 83, 157]
>>> invert([0.7, 0.2, 0.6])
[0.3, 0.8, 0.4]

>>> matrix = [[234, 124, 28], [36, 45, 179]]
>>> convertBitmap(bitmap=matrix, converter=invert)
[[21, 131, 227], [219, 210, 76]]

>>> matrix = [[[0.2, 0.4, 0.6], [0.5, 0.9, 0.0]], [[0.5, 0.7, 0.1], [0.4, 0.3, 1.0]]]
>>> convertBitmap(matrix)
[[[0.04, 0.16, 0.36], [0.25, 0.81, 0.0]], [[0.25, 0.49, 0.01], [0.16, 0.09, 1.0]]]
>>> convertBitmap(bitmap=matrix, converter=color2gray)
[[0.363, 0.6778], [0.5718, 0.4097]]
>>> convertBitmap(bitmap=matrix, converter=invert)
[[[0.8, 0.6, 0.4], [0.5, 0.1, 1.0]], [[0.5, 0.3, 0.9], [0.6, 0.7, 0.0]]]