我生成了pandas.DataFrame类型的RGB图像。使用matplotlib.pyplot.imshow()可以很好地显示图像。现在,我要将图像保存到本地磁盘。
我浏览了matplotlib的文档,发现ply.imsave()可以做到这一点。但是imsave()没有可以设置轴范围的参数。
我尝试了plt.axis(),但是它不起作用。请问这里有什么提示吗?
代码如下:
def _load_img(self, df):
imgUnpadded = np.zeros((3, self.img_x_max, self.img_y_max))
imgUnpadded[0,:,:],xedges,yedges = np.histogram2d(df['img_x'],
df['img_y'],
bins = [self.img_x_max, self.img_y_max])
imgUnpadded[1,:,:],xedges,yedges = np.histogram2d(df['img_x'],
df['img_y'],
bins = [self.img_x_max, self.img_y_max],
weights = df['u'])
imgUnpadded[2,:,:],xedges,yedges = np.histogram2d(df['img_x'],
df['img_y'],
bins = [self.img_x_max, self.img_y_max],
weights = df['v'])
return imgUnpadded
def _img_normlization(self, imgUnpadded, ymax, xmax):
if 'pad' in self.img_params:
pad = self.img_params['pad']
else:
pad = 50
img0 = np.pad(imgUnpadded[0,:,:],pad,'constant',constant_values = 0)
img = np.zeros((3, xmax+2*pad, ymax+2*pad))
for ic in range(1,3):
img[ic,:,:] = np.pad(imgUnpadded[ic,:,:],pad,'constant',constant_values = 0)
img[ic,:,:] = np.nan_to_num(img[ic,:,:]/img0)
img[ic,:,:] -= img[ic,:,:].min()
img[ic,:,:] /= img[ic,:,:].max()
img0 = np.nan_to_num(img0/img0)
img[0,:,:] = img0
img = np.transpose(img,(1,2,0))
return img
def _save_img(self, img, i, day, actual_dep_time):
if 'start_index' in self.img_params:
output_fn = '%06d' % (i+self.img_params['start_index']) + '_' + day + '_' + actual_dep_time + '.png'
else:
output_fn = '%06d' % (i+1) + '_' + day + '_' + actual_dep_time + '.png'
output_fp = os.path.join(self.save_base_fp, output_fn)
plt.axis([0, self.img_x_max, 0, self.img_y_max])
plt.imsave(output_fp, np.transpose(img,(1,0,2)), origin='lower')
这三个功能将一一运行。 _load_img()的输入是一个np.DataFrame,它具有4列['imgx','img_y','u','v']