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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15261851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:38:46  来源:igfitidea点击:

100x100 image with random pixel colour

pythonimagematplotlibpython-imaging-librarydraw

提问by IceDragon

I'm trying to make a 100x100image with each pixel being a different random colour, like this example:

我正在尝试制作100x100每个像素为不同随机颜色的图像,如下例所示:

enter image description here

在此处输入图片说明

I've tried to use matplotlibbut I'm not having much luck. Should I maybe be using PIL?

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

采纳答案by Hooked

This is simple with numpyand pylab. You can set the colormap to be whatever you like, here I use spectral.

这很简单,用numpypylab。您可以将颜色图设置为您喜欢的任何内容,这里我使用光谱。

from pylab import imshow, show, get_cmap
from numpy import random

Z = random.random((50,50))   # Test data

imshow(Z, cmap=get_cmap("Spectral"), interpolation='nearest')
show()

enter image description here

在此处输入图片说明

Your target image looks to have a grayscale colormap with a higher pixel density than 100x100:

您的目标图像看起来具有像素密度高于 100x100 的灰度颜色图:

import pylab as plt
import numpy as np

Z = np.random.random((500,500))   # Test data
plt.imshow(Z, cmap='gray', interpolation='nearest')
plt.show()

enter image description here

在此处输入图片说明

回答by heltonbiker

If you want to create an image file (and display it elsewhere, with or without Matplotlib), you could use Numpy and PIL as follows:

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

import numpy, Image

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

The idea here is to create a numeric array, convert it to a RGB image, and save it to file. If you want grayscale image, you should use convert('L')instead of convert('RGBA').

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

Hope this helps

希望这可以帮助

回答by PaulMcG

I wanted to write some simple BMP files, so I researched the format and wrote a very simple bmp.py module:

我想写一些简单的 BMP 文件,所以我研究了格式并写了一个非常简单的bmp.py 模块

# get bmp.py at http://www.ptmcg.com/geo/python/bmp.py.txt
from bmp import BitMap, Color
from itertools import product
from random import randint, choice

# make a list of 256 colors (all you can fit into an 8-bit BMP)
colors = [Color(randint(0,255), randint(0,255), randint(0,255)) 
                for i in xrange(256)]

bmp = BitMap(100,100)
for x,y in product(xrange(100),xrange(100)):
    bmp.setPenColor(choice(colors))
    bmp.plotPoint(x,y)

bmp.saveFile("100x100.bmp", compress=False)

Sample 100x100.bmp:

示例 100x100.bmp:

100x100.bmp

100x100.bmp

For a slightly larger pixel size, use:

对于稍大的像素尺寸,请使用:

PIXEL_SIZE=5
bmp = BitMap(PIXEL_SIZE*100,PIXEL_SIZE*100)
for x,y in product(xrange(100),xrange(100)):
    bmp.setPenColor(choice(colors))
    bmp.drawSquare(x*PIXEL_SIZE,y*PIXEL_SIZE,PIXEL_SIZE,fill=True)

filename = "%d00x%d00.bmp" % (PIXEL_SIZE,PIXEL_SIZE)
bmp.saveFile(filename)

500x500.bmp

500x500.bmp

You may not want to use bmp.py, but this shows you the general idea of what you'll need to do.

您可能不想使用 bmp.py,但这向您展示了您需要做什么的总体思路。

回答by user2145646

I believe the colour map of that array to be bone, i.e.

我相信该数组的颜色图是骨骼,即

#import the modules
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

rand_array=np.random.rand(550,550) #create your array
plt.imshow(rand_array,cmap=cm.bone) #show your array with the selected colour
plt.show() #show the image

Should produce what you're looking for :)

应该产生你正在寻找的东西:)

EDIT: Just change 550 to 100 if you want a 100x100 array

编辑:如果你想要一个 100x100 的数组,只需将 550 更改为 100

回答by banderlog013

import numpy as np
import matplotlib.pyplot as plt

img = (np.random.standard_normal([28, 28, 3]) * 255).astype(np.uint8)
plt.imshow(img)

Will give you this 28x28 RGB image:

会给你这个 28x28 RGB 图像:

img

图片