python debug时需要图像化展示数据,有三种方法。
方法一:t是值在[0, 255]之间的numpy数组,形状为 [ x ∗ x ∗ 3 ] [x*x*3] [x∗x∗3],其中3为channel数。(使用t.permute(1,2,0)变换通道,使用np.squeeze(t)去掉冗余维度)
import cv2
cv2.imwrite("1.jpg", t)
方法二:t是值在[0, 1]之间、或在[0, 255]之间的numpy数组,形状为 [ x ∗ x ∗ 3 ] [x*x*3] [x∗x∗3]
import matplotlib.pyplot as plt
plt.imshow(t)
plt.savefig('2.jpg')
方法三:t是PIL的一个实例,形状为 [ 3 ∗ x ∗ x ] [3*x*x] [3∗x∗x]
from PIL import Image
img=Image.open('d:/t.png')
img.save('3.jpg')