/
StringToPng.py
51 lines (43 loc) · 1.71 KB
/
StringToPng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pylint: disable=no-init,invalid-name
import mantid
class StringToPng(mantid.api.PythonAlgorithm):
def category(self):
""" Category
"""
return "DataHandling\\Plots"
def name(self):
""" Algorithm name
"""
return "StringToPng"
def summary(self):
return "Creates an image file containing a string."
def checkGroups(self):
return False
def PyInit(self):
#declare properties
self.declareProperty("String","", mantid.kernel.StringMandatoryValidator(),"String to plot")
self.declareProperty(mantid.api.FileProperty('OutputFilename', '', action=mantid.api.FileAction.Save, extensions = ["png"]),
doc='Name of the image file to savefile.')
def PyExec(self):
ok2run=''
try:
import matplotlib
except ImportError:
ok2run='Problem importing matplotlib'
from distutils.version import LooseVersion
if LooseVersion(matplotlib.__version__)<LooseVersion("1.2.0"):
ok2run='Wrong version of matplotlib. Required >= 1.2.0'
if ok2run!='':
raise RuntimeError(ok2run)
matplotlib.use("agg")
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(.1,.1))
ax1=plt.axes(frameon=False)
ax1.text(0.,1,self.getProperty("String").valueAsStr.decode('string_escape'),va="center",fontsize=16)
ax1.axes.get_xaxis().set_visible(False)
ax1.axes.get_yaxis().set_visible(False)
plt.show()
filename = self.getProperty("OutputFilename").value
plt.savefig(filename,bbox_inches='tight')
plt.close(fig)
mantid.api.AlgorithmFactory.subscribe(StringToPng)