Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

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

like image 812
DannyD Avatar asked Nov 30 '13 19:11

DannyD


People also ask

How do you create a bitmap in Python?

19 Python code examples are found related to " create bitmap". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. def CreateBitmapIndirect(lpbm): _CreateBitmapIndirect = windll.

What is bitmap in Python?

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. A bitmap is an array of binary data representing the values of pixels in an image.

How do I load a BMP image in Python?

Anyway using a C library to load BMP may work e.g. http://code.google.com/p/libbmp/ or http://freeimage.sourceforge.net/, and C libraries can be easily called from python e.g. using ctypes or wrapping it as a python module.


Video Answer


1 Answers

This is from 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()
like image 66
ghostbust555 Avatar answered Oct 05 '22 05:10

ghostbust555