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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20304438/
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 20:08:16  来源:igfitidea点击:

how can I use the python imaging library to create a bitmap

pythonbitmappython-imaging-library

提问by R Doolabh

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 列表,我想制作数据的图形图片。也许是一个由 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 的问题)

采纳答案by ghostbust555

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()