如何使用python映像库创建位图

时间:2021-02-03 00:25:56

I have a 2d list in python, and I want to make a graphical pic of the data. Maybe a n by m column grid where each square is a different color of grey depending on the value in my 2d list.

我在python中有一个2d列表,我想把数据做成图形化的图片。也许是一个n×m的列网格,其中每个方块都是不同颜色的灰色,取决于我的2d列表中的值。

However, I can't seem to figure out how to create images using PIL. This is some of the stuff I've been messing with:

然而,我似乎不知道如何使用PIL创建图像。这是我一直在搞的一些东西:

def createImage():
    img = Image.new('L', (100,100), 'white')
    img.save('test.bmp')

    for i in range(0,15):
        for j in range(0,15):
            img.putpixel((i,j), (255,255,255))

However, I'm getting an error saying that an integer is required (problem on the line with the putpixel)

但是,我得到一个错误,说需要一个整数(在与putpixel的行上有问题)

1 个解决方案

#1


15  

This is from http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels:

这是来自http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels

from PIL import Image

img = Image.new( 'RGB', (255,255), "black") # Create a new black image
pixels = img.load() # Create the pixel map
for i in range(img.size[0]):    # For every pixel:
    for j in range(img.size[1]):
        pixels[i,j] = (i, j, 100) # Set the colour accordingly

img.show()

#1


15  

This is from http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels:

这是来自http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels

from PIL import Image

img = Image.new( 'RGB', (255,255), "black") # Create a new black image
pixels = img.load() # Create the pixel map
for i in range(img.size[0]):    # For every pixel:
    for j in range(img.size[1]):
        pixels[i,j] = (i, j, 100) # Set the colour accordingly

img.show()