]> git.lyx.org Git - features.git/blob - lib/scripts/convertDefault.py
Fix command line output of convertDefault.py for ImageMagick
[features.git] / lib / scripts / convertDefault.py
1 # -*- coding: utf-8 -*-
2
3 # file convertDefault.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # \author Herbert Voß
8 # \author Bo Peng
9
10 # Full author contact details are available in file CREDITS.
11
12 # The default converter if no other has been defined by the user from the
13 # Conversion->Converter tab of the Preferences dialog.
14
15 # The user can also redefine this default converter, placing their
16 # replacement in ~/.lyx/scripts
17
18 # converts an image $2 (format $1) to $4 (format $3)
19 from __future__ import print_function
20 import os, re, sys
21
22 PY2 = sys.version_info[0] == 2
23
24 # We may need some extra options only supported by recent convert versions
25 re_version = re.compile(r'^Version:.*ImageMagick\s*(\d*)\.(\d*)\.(\d*).*$')
26 # imagemagick 7
27 command = 'magick'
28 fout = os.popen('magick -version 2>&1')
29 output = fout.readline()
30 if fout.close() != None:
31     # older versions
32     # caution: windows has a convert.exe for converting file systems
33     command = 'convert'
34     fout = os.popen('convert -version 2>&1')
35     output = fout.readline()
36     fout.close()
37 if not PY2:
38     output = output.decode()
39
40 version = re_version.match(output)
41
42 # Imagemagick by default
43 im = False
44 gm = False
45
46 if version != None:
47     major = int(version.group(1))
48     minor = int(version.group(2))
49     patch = int(version.group(3))
50     version = hex(major * 65536 + minor * 256 + patch)
51     im = True
52 else:
53     # Try GraphicsMagick
54     re_version = re.compile(r'^GraphicsMagick.*http:..www.GraphicsMagick.org.*$')
55     version = re_version.match(output)
56     if version != None:
57         gm = True
58
59 # IM >= 5.5.8 separates options for source and target files
60 # See http://www.imagemagick.org/Usage/basics/#why
61 if im or gm:
62     sopts = "-depth 8"
63     topts = ""
64 elif sys.platform == 'darwin':
65     command = 'lyxconvert'
66
67 # If supported, add the -define option for pdf source formats
68 if sys.argv[1] == 'pdf' and (version >= 0x060206 or gm):
69     sopts = '-define pdf:use-cropbox=true ' + sopts
70
71 # If supported, add the -flatten option for ppm target formats (see bug 4749)
72 if sys.argv[3] == 'ppm' and (im and version >= 0x060305 or gm):
73     topts = '-flatten'
74
75 # print (command, sys.argv[2], sys.argv[4], file= sys.stdout)
76 if (im or gm) and os.system(r'%s %s "%s" %s "%s"' % (command, sopts, sys.argv[2], topts, sys.argv[3] + ':' + sys.argv[4])) != 0:
77     print (sys.argv[0], 'ERROR', file= sys.stderr)
78     print ('Execution of "%s" failed.' % command, file= sys.stderr)
79     sys.exit(1)
80 elif not im and not gm and sys.platform == 'darwin' and os.system(r'%s "%s" "%s"' % (command, sys.argv[2], sys.argv[4])) != 0:
81     print (sys.argv[0], 'ERROR', file= sys.stderr)
82     print ('Execution of "%s" failed.' % command, file= sys.stderr)
83     sys.exit(1)