tags:

views:

777

answers:

3

I wish to draw an image based on computed pixel values, as a means to visualize some data. Essentially, I wish to take a 2-dimensional matrix of color triplets and render it.

Do note that this is not image processing, since I'm not transforming an existing image nor doing any sort of whole-image transformations, and it's also not vector graphics as there is no pre-determined structure to the image I'm rendering- I'm probably going to be producing amorphous blobs of color one pixel at a time.

I need to render images about 1kx1k pixels for now, but something scalable would be useful. Final target format is PNG or any other lossless format.

I've been using PIL at the moment via ImageDraw's draw.point , and I was wondering, given the very specific and relatively basic features I require, is there any faster library available?

+1  A: 

I think you use Pil to generate an image file on the disk, and you later load it with an image reader software.

You should get a small speed improvement by rendering directly the picture in memory (you will save the cost of writing the image on the disk and then re-loading it). Have a look at this thread http://stackoverflow.com/questions/326300/python-best-library-for-drawing for how to render that image with various python modules.

I would personally try wxpython and the dc.DrawBitmap function. If you use such a module rather than an external image reader you will have many benefits:

  • speed
  • you will be able to create an interactive user interface with buttons for parameters.
  • you will be able to easily program a Zoomin and Zoomout function
  • you will be able to plot the image as you compute it, which can be quite useful if the computation takes a lot of time
Mapad
+3  A: 
import Image
im= Image.new('RGB', (1024, 1024))
im.putdata([(255,0,0), (0,255,0), (0,0,255)])
im.save('test.png')

Puts a red, green and blue pixel in the top-left of the image.

im.fromstring() is faster still if you prefer to deal with byte values.

bobince
+4  A: 

If you have numpy and scipy available (and if you are manipulating large arrays in Python, I would recommend them), then the scipy.misc.pilutil.toimage function is very handy. A simple example:

import numpy as np
import scipy.misc.pilutil as smp

# Create a 1024x1024x3 array of 8 bit unsigned integers
data = np.zeros( (1024,1024,3), dtype=np.uint8 )

data[512,512] = [254,0,0]       # Makes the middle pixel red
data[512,513] = [0,0,255]       # Makes the next pixel blue

img = smp.toimage( data )       # Create a PIL image
img.show()                      # View in default viewer

The nice thing is toimage copes with diferent data types very well, so a 2d array of floating point numbers gets sensibly converted to greyscale etc.

You can download numpy and scipy from http://www.scipy.org/SciPy.

BADC0DE
Thanks. I am already using numpy quite heavily :) This pointer is just what I needed. I might compare speeds if this becomes a bottleneck, but for now this suits me well.
saffsd