python - 具有随机像素颜色的 100x100 图像

标签 python image matplotlib python-imaging-library draw

我正在尝试制作一个 100x100 图像,每个像素都是不同的随机颜色,就像这个例子:

enter image description here

我尝试过使用 matplotlib,但运气不佳。我应该使用 PIL 吗?

最佳答案

如果你想创建一个图像文件(并在其他地方显示它,有或没有 Matplotlib),你可以使用 NumPy 和 Pillow,如下所示:

import numpy
from PIL import Image

imarray = numpy.random.rand(100,100,3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('RGBA')
im.save('result_image.png')

这里的想法是创建一个数值数组,将其转换为 RGB 图像,并将其保存到文件中。如果你想要灰度图像,你应该使用 convert('L') 而不是 convert('RGBA')

关于python - 具有随机像素颜色的 100x100 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15261851/

相关文章:

python - 使用 python 中的键访问 csv 行中的每个单元格

python - matplotlib 图例 : I see everything twice

python - 尝试使用 matplotlib 绘制 OpenCV 的 MSER 区域

Python:绘制稀疏矩阵

python - 使用 Python 和 MySQL 中的简单队列系统并行调整大量图像的大小

python - 如何将带符号的 32 位整数转换为无符号的 32 位整数?

python - 在 python 中,当使用 sqlite3 时,数据库物理存储在哪里?

image - Abbyy Finereader 命令行用法/python 用法?

javascript - jQuery + Fancybox HREF 参数不刷新/缓存

image - LinkedIn API 不返回群组帖子和公司共享的 SSL 图像