在Python中调整图像大小

时间:2015-11-07 21:51:52

标签: python matplotlib

我使用this question中的代码将一些原始图片转换为png。

import matplotlib.pyplot as plt
import numpy as np

# Parameters.
input_filename = "JPCLN001.IMG"
shape = (2048, 2048) # matrix size
dtype = np.dtype('>u2') # big-endian unsigned integer (16bit)
output_filename = "JPCLN001.PNG"

# Reading.
fid = open(input_filename, 'rb')
data = np.fromfile(fid, dtype)
image = data.reshape(shape)

# Display.
plt.imshow(image, cmap = "gray")
plt.savefig(output_filename)
plt.show()

问题是,我预计png大小为2048x2048,但我得到的只是500x500以下的图像。关于如何解决这个问题的任何建议?

1 个答案:

答案 0 :(得分:1)

如果您只想将数组保存为.png而不绘制它,可以使用matplotlib.image.imsave

import numpy as np
from matplotlib import pyplot as plt

# some random data
img = np.random.randint(256, size=(2048, 2048))

# creates a 2048 x 2048 .png image
plt.imsave('img.png', img, cmap='gray')