]> git.tdb.fi Git - libs/gl.git/blob - scripts/maketex.py
Adapt exporter to Blender 2.63
[libs/gl.git] / scripts / maketex.py
1 #!/usr/bin/python
2
3 def escape(str):
4         result=""
5         for c in str:
6                 if c=='"':
7                         result+='\\"'
8                 elif c=='\\':
9                         result+='\\\\'
10                 elif ord(c)<0x20:
11                         result+="\\%03o"%ord(c)
12                 else:
13                         result+=c
14         return result;
15
16 def make_tex(fn):
17         import Image
18
19         img=Image.open(fn)
20
21         fmt="".join(img.getbands())
22         if fmt=="LA":
23                 fmt="LUMINANCE_ALPHA"
24         elif fmt=="L":
25                 fmt="LUMINANCE"
26
27         result="storage %s %d %d;\n"%(fmt, img.size[0], img.size[1])
28         result+="min_filter LINEAR;\n"
29         result+="raw_data \""
30         data=list(img.getdata())
31         for y in range(img.size[1]):
32                 i=(img.size[1]-1-y)*img.size[0]
33                 result+=escape("".join(["".join([chr(v) for v in p]) for p in data[i:i+img.size[0]]]))
34         result+="\";\n"
35
36         return result
37
38 if __name__=="__main__":
39         import sys
40         import os
41
42         if len(sys.argv)<2:
43                 print "Usage: %s <image>"%sys.argv[0]
44         else:
45                 out=file(os.path.splitext(sys.argv[1])[0]+".tex", "w")
46                 out.write(make_tex(sys.argv[1]))