]> git.lyx.org Git - lyx.git/blob - lib/scripts/pic2ascii.py
ws change
[lyx.git] / lib / scripts / pic2ascii.py
1 #!/usr/bin/python
2 # This script converts a raster format picture into an ascii representation
3 # with the suffix .asc
4
5 import sys
6 import os
7 import string
8
9 pid = os.fork()
10 if pid == 0:
11         os.execvp("convert", ["convert", sys.argv[1], "temporary_filename_that_is_long.gif"])
12         print "Could not run convert"
13         os.exit(1)
14 os.wait()
15
16 os.system("identify temporary_filename_that_is_long.gif > temp.dim")
17
18 fp = open("temp.dim", "r")
19 line = fp.readline()
20 lines = string.split(line,' ')
21 dims = string.split(lines[1],'x')
22 xsize = float(dims[0])
23 ysize = float(string.split(dims[1],'+')[0])
24
25 aspect_ratio = xsize / ysize
26
27 if len(sys.argv) > 2:
28         resulting_x = int(sys.argv[2])
29 else:
30         resulting_x = 40
31 resulting_y = int(resulting_x / aspect_ratio)
32
33 os.system("echo s | gifscii temporary_filename_that_is_long.gif %d %d" % (resulting_x, resulting_y))
34
35 os.system("tail +3 temporary_filename_that_is_long.asc > temporary_filename_that_is_long2.asc")
36
37 pid = os.fork()
38 if pid == 0:
39         os.execvp("mv", ["mv", "temporary_filename_that_is_long2.asc", os.path.splitext(sys.argv[1])[0] + ".asc"])
40         print "Could not rename file"
41         os.exit(1)
42 os.wait(pid)
43
44 os.system("rm temporary_filename_that_is_long.gif temporary_filename_that_is_long.asc")
45