【发布时间】:2021-11-16 23:52:35
【问题描述】:
我有一些数据。我将其可视化,然后将其另存为图像。
import cv2
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[1,2,0,1],
[0,1,2,1],
[0,0,2,1]])
fig, ax = plt.subplots()
ax.imshow(data)
ax.axis('off')
fig.savefig("test.png", bbox_inches='tight', pad_inches=0)
接下来,我加载图像并读取形状:
img = cv2.imread('test.png')
print(img.shape)
输出:
(217, 289, 3)
但我想保持原来的分辨率和我的预期输出:
(3, 4, 3)
有什么办法吗?
更新:
使用 dpi=1:
data = np.array([
[1,2,0,1],
[0,1,2,1],
[0,0,2,1],
[1,0,2,1],
[4,1,0,2],
])
fig, ax = plt.subplots()
ax.imshow(data)
ax.axis('off')
fig.savefig("test.png", bbox_inches='tight', pad_inches=0, dpi = 1)
img = cv2.imread('test.png')
img.shape
print(data.shape, img.shape)
输出:
(5, 4)
(3, 2, 3)
【问题讨论】:
-
看起来你想在保存图窗时设置
dpi。我想你想要dpi=1
标签: python numpy matplotlib cv2