我如何可以使用Python图像库创建位图 [英] how can I use the python imaging library to create a bitmap

查看:584
本文介绍了我如何可以使用Python图像库创建位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中的二维表,我想使数据的图形照片。也许由M列电网n,其中每平方米是取决于我的2D列表中的值不同的颜色灰色的。

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.

不过,我似乎无法弄清楚如何使用PIL创建图像。这是一些我一直在搞乱东西:

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:

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

不过,我得到一个错误说一个整数需要(在用的putpixel线的问题)

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

推荐答案

这是从<一个href=\"http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels\">http://en.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels

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

这篇关于我如何可以使用Python图像库创建位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆