我有一个数组(50(行)乘33(列))。我将其导入python上的numpy数组。数组上的值的范围是0到4096,因此我将其缩放到0-255,然后尝试从中创建灰度图像并保存。
import numpy
from PIL import Image
data = numpy.loadtxt('result.txt',delimiter='\t',dtype=None)
rescaled = (255.0 / 4096 * (data)).astype(numpy.uint8)
image = Image.fromarray(rescaled)
image.save('test_fullsize.jpeg', quality=95)
这不会输出任何错误,但是生成的图像仅为50x33像素。这意味着图片非常小(我可以看到它确实是我正在寻找的图像类型,但是它非常非常小)
在保存之前,我尝试通过添加以下代码行来调整图像的大小:
image = image.resize((480, 640), Image.ANTIALIAS)
但是,这会输出非常像素化(质量差)的480,640图像。我需要此图像具有适当的分辨率/大小,才能将其放置在海报上。
谢谢你。
据我了解,您正在尝试将矩阵中的数字显示为不同的灰色阴影,并使其足够大以适合海报。因此,就像@pandita一样,我也将使用Matplotlib,因为这很容易做到。
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
height, width = 50, 33 #in pixels
spines = 'left', 'right', 'top', 'bottom'
labels = ['label' + spine for spine in spines]
tick_params = {spine : False for spine in spines}
tick_params.update({label : False for label in labels})
img = np.random.randint(0, 4097, (height, width))
img *= 255. / 4096
desired_width = 8 #in inches
scale = desired_width / float(width)
fig, ax = plt.subplots(1, 1, figsize=(desired_width, height*scale))
img = ax.imshow(img, cmap=cm.Greys_r, interpolation='none')
#remove spines
for spine in spines:
ax.spines[spine].set_visible(False)
#hide ticks and labels
ax.tick_params(**tick_params)
#preview
plt.show()
#save
fig.savefig('test.png', dpi=300)
产生以下图像...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句