本文介绍了如何在Python中将二进制数组写为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个二进制数数组:

I have an array of binary numbers in Python:

data = [0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1...]

我想取出这些数据并将其另存为位图,其中"0"对应于白色,而"1"对应于黑色.我知道数组中有2500个数字,对应于50x50位图.我已经下载并安装了PIL,但不确定如何将其用于此目的.如何将该数组转换为相应的图像?

I would like to take this data out and save it as a bitmap, with a '0' corresponding to white and a '1' corresponding to black. I know that there are 2500 numbers in the array, corresponding to a 50x50 bitmap. I've downloaded and installed PIL, but I'm not sure how to use it for this purpose. How can I convert this array into the corresponding image?

推荐答案

numpymatplotlib的实现方式是:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
plt.imsave('filename.png', np.array(data).reshape(50,50), cmap=cm.gray)

请参见

这篇关于如何在Python中将二进制数组写为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:13