我正在尝试制作一个 100x100
图像,每个像素都是不同的随机颜色,就像这个例子:
我尝试过使用 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/