[Tutor] Raw Image Format

bri.wood bri.wood@ntlworld.com
Tue Jun 17 10:39:02 2003


bri.wood wrote:

> Hi, I'm new to python (waiting for Learning Python to be delivered 
> from Amazon), but I have used it to post-process some images for a 
> povray animation.
> Now my question, is there any way to get python to save an image in 
> the raw format? I've looked through PIL and Imagemagick, but these 
> don't seem to support the raw format.
>
> Thanks for your time.
>
> Bri
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
I hope this is ok, replying to my own message, it doesn't mean I'm 
talking to myself, just passing on info to the list. Been wanting to 
post here for a while, it seems quite friendly :)
Well I've worked out the answer to my problem and learnt abit along the way.
I'm so glad that I said PIL didn't seem to have the answer....cos it 
did, the getdata() method was what I was after. Explains it in the PIL 
handbook too, but I was just looking at the formats that PIL saved in.
I learn more from my mistakes than anything, like save not working on 
getdata() so i did what is said in the handbook and converted it to a 
list, still wouldn't work, then I converted it to a string and it worked!!!
But it was in the wrong format....so I spent sometime learning about 
string.replace....then how to convert a number to ascii. Then it hit me, 
didn't one of the error messages say something about not accepting a 
list and I was doing all this replace stuff cos the data was in a 
list....and it clicked, one of the things I liked about python is that 
you can do loopy things with lists.

Here is what I ended up with, its not neat or pretty, but it works, any 
more ideas would be welcomed.

import Image
import ImageOps
im = Image.open("15.jpg")
im = ImageOps.grayscale(im)
mydata = list(im.getdata())
myfile = open('mytest.raw','wb')
for x in mydata:
    myfile.write(chr(x))
myfile.close(