forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringToPng.py
60 lines (50 loc) · 2.1 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
52
53
54
55
56
57
58
59
60
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=no-init,invalid-name
import mantid
class StringToPng(mantid.api.PythonAlgorithm):
def category(self):
""" Category
"""
return "DataHandling\\Plots"
def seeAlso(self):
return ["SavePlot1D"]
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, bytearray(self.getProperty("String").valueAsStr, 'utf-8').decode('unicode_escape'), va="center", fontsize=16)
ax1.axes.get_xaxis().set_visible(False)
ax1.axes.get_yaxis().set_visible(False)
filename = self.getProperty("OutputFilename").value
fig.savefig(filename, bbox_inches='tight')
plt.close(fig)
mantid.api.AlgorithmFactory.subscribe(StringToPng)