generate image files

By Hal Canary, 2012-03-23 12:00:00 (link)

#!/usr/bin/python

import Image
import numpy

X = 1024
Y = 768
filename = '/tmp/x.png'

ima = numpy.zeros((Y,X,4), dtype=numpy.uint8)
ima[:,:,3] = 255 # make opaque
Image.fromarray(ima,mode='RGBA').save(filename)
For example:
#!/usr/bin/env python
import Image
import numpy
N, K = (16,16), (256, 256)
fn = 'random_pixels.png'
im = numpy.random.random_integers(
    0,255,(N[0],N[1],3)).astype('uint8')
Image.fromarray(im,mode='RGB').resize(K).save(fn)
print 'Wrote to', fn

(back)