Aug-16-2019, 08:34 PM
So Ive had a first step at using numpy. Code is below, Im sure its rather crude and any critique is very welcome.
The time 'signature' in terms of total run time for various n (to give a nxn image) is broadly similar to that for the Python2.7 char string catenation method.
https://gyazo.com/2df2fbef43953fa1c14831abbd63925c
The core code fragments are:
The time 'signature' in terms of total run time for various n (to give a nxn image) is broadly similar to that for the Python2.7 char string catenation method.
https://gyazo.com/2df2fbef43953fa1c14831abbd63925c
The core code fragments are:
PX = np.zeros((size2, Size), dtype=(int,3)) # size2 is Size rounded up to mult of 4 for Y in range(0, XY[1]): # (BMPs are L to R from the bottom L row) for X in range(0, XY[0]): # dummy code to make square rainbow, as opposed to real contour data x = floor((255 * X)/XY[0]) y = floor((255 * Y)/XY[1]) (r,g,b) = [x, y, 128] #Colour(data[x ,y]) PX[X,Y] = (b,r,g) # could probably be a 1 liner vec = np.ravel(PX, order='C') vec = [int(v) for v in vec] vec = bytes(vec)Here is the new code in full to build the pixel array (a bit messy because f logging & tracking info):
def BuildImage2(name, XY): # using numpy array global t01 n0 = 0 t00 = time.process_time() t01 = t00 def delTime(): global t01 t02 = time.process_time() t = t02-t01 t01 = t02 return t chtName2 = path+'cht_'+name+'2.bmp' print("drawing "+chtName2) hdr = bmpHdr(XY) #print(hdr) # pretty sure theres a neater way to do this row_mod = (hdr['width']*hdr['colordepth']/8) % 4 if row_mod == 0: pad = 0 else: pad = (4 - row_mod) size2 = Size+pad PX = np.zeros((size2, Size), dtype=(int,3)) t02 = time.process_time() print("empty array tim: {0:.3f} secs".format(delTime())) for Y in range(0, XY[1]): # (BMPs are L to R from the bottom L row) for X in range(0, XY[0]): x = floor((255 * X)/XY[0]) y = floor((255 * Y)/XY[1]) (r,g,b) = [x, y, 128] #Colour(data[x ,y]) PX[X,Y] = (b,r,g) #if(0 == Y % 100 or Y == 0): # print("{0:5d} time = {0:6.3f}".format(XY[0]-Y, delTime())) print("PX populated") print( PX.shape) t02 = time.process_time() print("poulated array tim: {0:.3f} secs".format(delTime())) vec = np.ravel(PX, order='C') print("PX ravelled, len={0}".format(len(vec))) print(type(vec[0])) print(vec[0:9]) vec = [int(v) for v in vec] vec = bytes(vec) print("PX bytes, len={0}".format(len(vec))) print(type(vec[0])) print(vec[0:9]) print("bytes array tim: {0:.3f} secs".format(delTime())) bmp_write(chtName2, hdr, vec)