如何将矩阵中的灰度值转换为Python中的图像? [英] How to convert grayscale values in matrix to an image in Python?

查看:1967
本文介绍了如何将矩阵中的灰度值转换为Python中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在形状矩阵24x24中有一组灰度值:

I have a set of grayscale values in matrix of shape 24x24:

masked=[[149 172 160 166 170 179 180 176 202 190 221 232 125 112 153 132 200 185
  191 231 227 101  85 127] ...

我尝试将此矩阵文件保存为灰度图像,如下所示:

And I try to save this matrix file to a grayscale image as follows:

im = Image.fromarray(masked_crop)
im.save('crop.png')

但不是那些在我的图像中,我得到一个大小为24x24的完整黑暗图像。
我哪里错了?

But instead of having those values in my image, I get a complete dark image of size 24x24. Where am I going wrong?

推荐答案

您可以使用matplotlib显示和保存图像

You can display and save an image with matplotlib

import numpy
from matplotlib import pyplot as plt
x = numpy.random.rand(10, 10)*255
plt.imshow(x, cmap='gray', interpolation='nearest', vmin=0, vmax=255)
plt.savefig('text.png')
plt.show()

这篇关于如何将矩阵中的灰度值转换为Python中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆