imagetools.py 2.05 KB
Newer Older
Yan's avatar
Yan committed
1
2
3
4
5
6
7
8
#!/usr/bin/env python3
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from io import BytesIO
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtPrintSupport
from PyQt5 import QtWidgets
9
from pathlib import Path
3Yan's avatar
3Yan committed
10
import copy
Yan's avatar
Yan committed
11
import prasopes.graphtools as gt
12
import prasopes.config as cf
Yan's avatar
Yan committed
13
14


15
def paint_image(mass_spec, spect, fn=None, painttarget=None):
16
    """generates QImage from mass spectrum"""
17
18
19
20
21
22
    configtype = "print/{}" if isinstance(
            painttarget, type(QtPrintSupport.QPrinter())) else "imggen/{}"
    xinch, yinch = [float(cf.settings().value(configtype.format(i)).replace(",",".")) for i
            in ("xinch", "yinch")]
    dpi, xtics = [int(cf.settings().value(configtype.format(i))) for i
            in ("dpi", "xtics")]
23
    paintfig = Figure(figsize=(xinch, yinch), dpi=dpi, constrained_layout=True)
Yan's avatar
Yan committed
24
25
26
27
    canvas = FigureCanvas(paintfig)
    printplot = paintfig.add_subplot(111)
    printplot.set_xlim(spect.get_xlim())
    printplot.set_ylim(spect.get_ylim())
Yan's avatar
Yan committed
28
    data = [line.get_data() for line in spect.lines]
3Yan's avatar
3Yan committed
29
    texts = copy.copy(mass_spec)
Yan's avatar
Yan committed
30
31
    if len(mass_spec['headers']) != 0:
        legend = spect.get_legend().get_texts()
3Yan's avatar
3Yan committed
32
        [gt.pop_plot(*line, printplot, texts, i, legend[i].get_text())
Yan's avatar
Yan committed
33
34
35
                for i,line in enumerate(data)]
        printplot.legend(loc=2)
    else:
3Yan's avatar
3Yan committed
36
        [gt.pop_plot(*line, printplot, texts, i)
Yan's avatar
Yan committed
37
                for i,line in enumerate(data)]
38
    printplot.locator_params(nbins=xtics, axis='x')
39
40
41
    if fn not in (None, [None]):
        printplot.set_title(Path(*Path(fn[0]).resolve().parts[-2:]),
                            loc="right")
Yan's avatar
Yan committed
42
43
44
45
46
47
48
    cache_file = BytesIO()
    paintfig.savefig(cache_file)
    cache_file.seek(0)
    image = QtGui.QImage.fromData(cache_file.read())
    return image


49
50
def clip_spect_img(mass_spec, spect, fn):
    image = paint_image(mass_spec, spect, fn=fn)
Yan's avatar
Yan committed
51
52
    [QtWidgets.QApplication.clipboard().setImage(image, i) for i in range(2)]
    #QtWidgets.QApplication.clipboard().setPixmap(QtGui.QPixmap.fromImage(image))