如何使用自定义值栅格化 2D canvas 上的 2D 点?
How to rasterize 2D points on a 2D canvas with custom values?
给定一个点列表 x_coords,y_coords
及其对应的 values
,我想将其光栅化为具有这些特定 values
的二维 canvas
。是否有 python 库可以执行此操作?作为尝试,我使用 PIL
,但是,它只允许我填充一个值:
draw = ImageDraw.Draw(canvas)
draw.point([*zip(x_coords, y_coords)], fill=1)
# Ideally I want to fill it with specific values:
# draw.point([*zip(x_coords, y_coords)], fill=values)
听起来正是您想要的,scipy.interpolate.griddata
。包括示例代码。基本上你需要:
# rearrange your coordinates into one array of rows
points = np.stack([x_coords, y_coords]).T
# the j-"notation" gives you 100 by 200 points in the 0..1 interval, or...
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
# this gives you the specified step
grid_x, grid_y = np.mgrid[0:1:0.01, 0:1:0.01]
# resample point data according to grid
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')
给定一个点列表 x_coords,y_coords
及其对应的 values
,我想将其光栅化为具有这些特定 values
的二维 canvas
。是否有 python 库可以执行此操作?作为尝试,我使用 PIL
,但是,它只允许我填充一个值:
draw = ImageDraw.Draw(canvas)
draw.point([*zip(x_coords, y_coords)], fill=1)
# Ideally I want to fill it with specific values:
# draw.point([*zip(x_coords, y_coords)], fill=values)
听起来正是您想要的,scipy.interpolate.griddata
。包括示例代码。基本上你需要:
# rearrange your coordinates into one array of rows
points = np.stack([x_coords, y_coords]).T
# the j-"notation" gives you 100 by 200 points in the 0..1 interval, or...
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
# this gives you the specified step
grid_x, grid_y = np.mgrid[0:1:0.01, 0:1:0.01]
# resample point data according to grid
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')