Raw images

Peter Hansen peter at engcorp.com
Tue Dec 20 15:09:53 EST 2005


Tuvas wrote:
> I have an image that is in a "raw" format, ei, no place markers to tell
> the dimensions, just a big group of numbers. I happen to know the
> dimension of this array from a different source. I would like to be
> able to display it. Is there a way to do this easily? I know that
> several libraries use a "raw" function, but I have little doubt that
> this differs somewhat from program to program. Thanks!

Assuming the numbers are stored as bytes in a file, this should work 
using PIL at http://www.pythonware.com/products/pil/

 >>> import Image
 >>> bytes = open('myfile', 'rb').read()
 >>> y = Image.fromstring('L', (xsize, ysize), x)
 >>> y.save('test.png', 'PNG')

The 'L' in the above is for bytes, and this obviously saves the image as 
a PNG file.  Lots of variation in the above is possible but your specs 
are fairly wide open so it's up to you to experiment and then maybe 
clarify your specs.

-Peter




More information about the Python-list mailing list