# Many ways to create a chess-like image in Python/Numpy

Contents

This demonstration illustrates many different ways to create a chess-like image in Python. Each way has a different concept, but most of them uses the property that each pixel is 0 or 1 depending if the sum of its coordinates is even or odd. The most efficient way is the technique using slices and the most general one is the one that uses indices. These examples below is a good introduction to program Python efficiently for image processing applications and verify that processing arrays in Python is very efficient.

The image that we want to generate can be describe in the following equation:

# Programming styles

We show below seven different programs, some very efficient as uses intrinsic implementation in Numpy, others not so efficient.

## C style programming

This is the traditional way that a C programmer would solve the problem - scan every pixel and apply the equation.

1. def chessCstyle(H,W): 2. f = empty((H,W),'uint8') 3. for row in range(H): 4. for col in range(W): 5. f[row,col] = (row+col)%2 6. return f 7. 8. print "C Style:\n", chessCstyle(4,8)

C Style: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## Image coordinates

This is a general way to create images when we can write the equation of each pixel value
based on its coordinates. The function *indices* is very handy to create arrays of each coordinate
make possible the array processing of the whole image without the explicit scanning of the pixels in the image.

1. def chessIndice(H,W): 2. row,col = indices((H,W), 'uint16') 3. return (row+col)%2 4. 5. print "Indices:\n", chessIndice(4,8)

Indices: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## image coordinates and broadcast

In the previous indices example, the arrays row and col are two-dimensional arrays. It is possible to use the broadcast facility of NumPy. We create a row as one column 2D array and col as a one row 2D array and let the arithmetic operation create the full 2D array using broadcast.

1. def chessIndiceBroad(H,W): 2. row = arange(H).reshape(H,1).astype('uint16') 3. col = arange(W).reshape(1,W).astype('uint16') 4. return (row+col)%2 5. 6. print "IndiceBroad:\n", chessIndiceBroad(4,8)

IndiceBroad: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## List implicit loop (list comprehension)

This solution uses the native implicit loop in list generation. The list is then converted to array.

1. import numpy as np 2. def chessIter(H,W): 3. f = [[ (row+col)%2 for col in range(W) ] for row in range(H) ] 4. return np.array(f,'uint8') 5. 6. print "Iter:\n", chessIter(4,8)

Iter: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## Slice

Slicing is a powerful index access to elements of an array available in *numpy*.
This solution is one of the most efficient. The image is initially create with zeros and
later filled with ones in the even lines and after in odd lines, always scanning the image
every two pixels.

1. def chessSlice(H,W): 2. f = zeros((H,W), 'uint8') 3. f[0::2,1::2] = 1 4. f[1::2,0::2] = 1 5. return f 6. 7. print "Slice:\n", chessSlice(4,8)

Slice: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## Line and column replication

This solution has three main parts: in the first a 4 pixel basic pattern is created; in the second
this pattern is replicated line-wise and the last and third part, the replication is done column-wise.
Use the functions *vstack* and *hstack*.

01. def chessRep(H,W): 02. p = array([[0,1], 03. [1,0]],'uint8') 04. fCol = p 05. 06. # First column 07. for row in range (1, H/2): 08. fCol = vstack((fCol , p)) 09. f = fCol 10. # Column replication 11. for col in range (1, W/2): 12. f = hstack((f, fCol)) 13. return f 14. 15. print "Replication:\n", chessRep(4,8)

Replication: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## Tile

This solution uses the properties of the Numpy function *tile*. It replicates the 4-pixel pattern over
the image according to the number of replication in the vertical and horizontal directions.

1. def chessTile(H,W): 2. p = array([[0,1], 3. [1,0]], 'uint8') 4. f = tile(p, (H/2, W/2)) 5. return f 6. 7. print "Tiling:\n", chessTile(4,8)

Tiling: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

## Resize

This solution uses the Numpy function *resize* to increase the image until the desired size, replicating
the initial raster image contents. We first create two lines of the image and then rezise it until the
final dimension.

1. def chessResize(H,W): 2. # create two lines and then apply resize 3. row1 = arange(W).astype('uint8') % 2 4. row2 = array([row1, 1 - row1]) 5. f = resize(row2, (H,W)) 6. return f 7. 8. print "Resize:\n", chessResize(4,8)

Resize: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]

# Comparing execution times

We compare the speed efficiency of the implementation above. We can see that slicing, tiling and resize are the most efficient
implementations because it is intrinsic to Numpy. Slicing is very efficient and is very recommending in most image processing
manipulation. Coordinate processing and replication have medium efficiency with coordinate processing being a very general
way to create images. Lastly list processing and C style programming present the worst efficiency because of its explicit
pixel scanning. For comparison, the result of the chess image is always of an array of type *uint8*, except for the coordinates method
where the result is in *uint16* as the coordinate values are greater than 255.

01. from time import time 02. import ia636 03. print '.. csv-table:: Execution time' 04. print """ :header: 'Program', 'Time (ms)' """ 05. print 06. 07. H,W = 2000,2000 08. funlist = (chessResize, chessSlice, chessTile, chessIndiceBroad, chessIndice, chessIter, chessRep, chessCstyle) 09. fundesc = ("Resize", "Slicing", "Tiling", "CoordBroad" , "Coordinates", "List processing", "Replication", "C style") 10. i=0 11. for fun in funlist: 12. t1 = time() 13. f = fun(H,W) 14. t2 = time() 15. print ' ',fundesc[i],',', round(1000*(t2-t1),2) 16. i = i+1 17. adshow(f*255, title='Chess like image, dimensions: %s' % (f.shape,))

'Program' | 'Time (ms)' |
---|---|

Resize | 6.7 |

Slicing | 9.96 |

Tiling | 27.68 |

CoordBroad | 65.38 |

Coordinates | 111.26 |

List processing | 729.48 |

Replication | 337.99 |

C style | 1151.62 |

# Conclusions

Numpy is very efficient for array processing and it is very suitable for image processing.
The recommended way of programming are: avoid explicit pixel scanning; use slicing as much as possible and use
array processing such as in the *coordinates* example. Pixel scanning programming style is not recommended.