IOLink Python 1.11.0
Loading...
Searching...
No Matches
checkerBoardFast.py

Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.

Faster method to create a checkerboard pattern in a memory image view The image is written tile by tile, using a buffer to write the tile values at once.

1import iolink
2
3from array import array
4
5import timeit
6
7
10def checkerBoard_Fast(sideLength):
11 shape = iolink.VectorXu64(sideLength, sideLength)
12 dt = iolink.DataTypeId.UINT8
13
14 # allocate the image in memory
15 image = iolink.ImageViewFactory.allocate(shape, dt)
16
17 tileCount = 10
18
19 # for a uint8 grayscale image, define the two color values to use
20 blackValue = 0
21 whiteValue = 255
22
23 # compute the size of a tile (width and height are the same)
24 tileSize = shape / tileCount
25 regionTile = iolink.RegionXu64.create_full_region(tileSize)
26
27 # allocate 2 buffers, one filled with black value, and the other with one value
28 # we use the computed region to know the needed size of buffers
29 blackBuf = array('B', [blackValue] * regionTile.element_count)
30 whiteBuf = array('B', [whiteValue] * regionTile.element_count)
31
32 # for each tile of the grid on X and Y axis
33 for tileX in range(0, tileCount):
34 for tileY in range(0, tileCount):
35 # compute the origin position of the region to write
36 origin = iolink.VectorXu64(tileX, tileY) * tileSize
37 regionTile = iolink.RegionXu64(origin, tileSize)
38 # write the black buffer or white buffer according to the tile index
39 if ( (tileX + tileY) % 2 == 0):
40 image.write_region(regionTile, blackBuf)
41 else:
42 image.write_region(regionTile, whiteBuf)
43
44 return image
45
46start = timeit.default_timer()
47img = checkerBoard_Fast(1000)
48stop = timeit.default_timer()
49print('CheckerBoard fast method created in: ', stop - start, "s")
50
51print("Generated ImageView:", str(img))
52
53
55
56# from PIL import Image
57# import numpy as np
58
59
62
63#img.save("checkerboard.png")
64#img.show()
65
66
67print("SUCCESS")