'Faster way of converting a dataframe of x,y,z values into an image?

I have a simple dataframe structure that looks like this:

print(scene_2d_df.head())

     x       y  z
0  963  1691.0  0
1  911  1881.0  0
2  837   864.0  1
3  785  1054.0  0
4  897    59.0  0

print(scene_2d_df.shape)

(2294591, 3)

Every row represents a white or black dot (1 or 0) in an image. The x and y columns are the pixel positions. The image is approx. 1200 x 1800 in this case. I have code which I believe works, but is running very slowly even on a modern machine. The approach is a bit brute-force.

def construct_image_from_df(df_1):
    xmax = int(df_1.max(axis=0)['x'])
    xmin = int(df_1.min(axis=0)['x'])
    ymax = int(df_1.max(axis=0)['y'])
    ymin = int(df_1.min(axis=0)['y'])
    zmax = int(df_1.max(axis=0)['z'])
    zmin = int(df_1.min(axis=0)['z'])
    
    print("xmin :: " + str(xmin) + " // xmax :: " + str(xmax)) # 1200-something
    print("ymin :: " + str(ymin) + " // ymax :: " + str(ymax)) # 1800-something
    print("zmin :: " + str(zmin) + " // zmax :: " + str(zmax)) # 1, all values 0 or 1
    
    img = np.zeros((xmax, ymax))
    
    length = df_1.shape[0] # number of rows
    for i in range(0, length):
        x, y, z = int(df_1.iloc[i]['x']), int(df_1.iloc[i]['y']), int(df_1.iloc[i]['z'])
        img[x - 1, y - 1] = z

    return img

Basically I am grabbing every row of the dataframe, and manually doing a pixel write into my 2D img array. It is very slow.

Is there a faster (maybe vectorized) way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source