-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimagefile.py
More file actions
183 lines (157 loc) · 6.05 KB
/
imagefile.py
File metadata and controls
183 lines (157 loc) · 6.05 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
"""
Image file dialog helpers
-------------------------
Overview
^^^^^^^^
The :py:mod:``.imagefile`` module provides helper functions for opening and
saving image files using Qt dialogs and plotpy's I/O features.
Ready-to-use open/save dialogs:
:py:func:`.imagefile.exec_image_save_dialog`
Executes an image save dialog box (QFileDialog.getSaveFileName)
:py:func:`.imagefile.exec_image_open_dialog`
Executes an image open dialog box (QFileDialog.getOpenFileName)
:py:func:`.imagefile.exec_images_open_dialog`
Executes an image*s* open dialog box (QFileDialog.getOpenFileNames)
Reference
^^^^^^^^^
.. autofunction:: exec_image_save_dialog
.. autofunction:: exec_image_open_dialog
.. autofunction:: exec_images_open_dialog
"""
from __future__ import annotations
import os.path as osp
import sys
import numpy as np
from qtpy import QtWidgets as QW
from qtpy.QtWidgets import QWidget # only to help intersphinx find QWidget
from plotpy import io
from plotpy.config import _
# ===============================================================================
# Ready-to-use open/save dialogs
# ===============================================================================
def exec_image_save_dialog(
parent: QWidget,
data: np.ndarray,
template: dict | None = None,
basedir: str = "",
app_name: str | None = None,
) -> str | None:
"""Executes an image save dialog box (QFileDialog.getSaveFileName)
Args:
parent: parent widget (None means no parent)
data: image pixel array data
template: image template (pydicom dataset) for DICOM files
basedir: base directory ('' means current directory)
app_name: application name (used as a title for an eventual
error message box in case something goes wrong when saving image)
Returns:
Filename if dialog is accepted, None otherwise
"""
saved_in, saved_out, saved_err = sys.stdin, sys.stdout, sys.stderr
sys.stdout = None
filename, _filter = QW.QFileDialog.getSaveFileName(
parent,
_("Save as"),
basedir,
io.iohandler.get_filters("save", dtype=data.dtype, template=template),
)
sys.stdin, sys.stdout, sys.stderr = saved_in, saved_out, saved_err
if filename:
filename = str(filename)
kwargs = {}
if osp.splitext(filename)[1].lower() == ".dcm":
kwargs["template"] = template
try:
io.imwrite(filename, data, **kwargs)
return filename
except Exception as msg:
import traceback
traceback.print_exc()
message = _("{filename} could not be written:\n{msg}").format(
filename=osp.basename(filename), msg=msg
)
QW.QMessageBox.critical(
parent, _("Error") if app_name is None else app_name, message
)
return
def exec_image_open_dialog(
parent: QWidget,
basedir: str = "",
app_name: str | None = None,
to_grayscale: bool = True,
dtype: np.dtype | None = None,
) -> tuple[str, np.ndarray] | None:
"""Executes an image open dialog box (QFileDialog.getOpenFileName)
Args:
parent: parent widget (None means no parent)
basedir: base directory ('' means current directory)
app_name: application name (used as a title for an eventual
error message box in case something goes wrong when saving image)
to_grayscale (bool | None): convert image to grayscale
dtype: data type of the image
Returns:
(filename, data) tuple if dialog is accepted, None otherwise
"""
saved_in, saved_out, saved_err = sys.stdin, sys.stdout, sys.stderr
sys.stdout = None
filename, _filter = QW.QFileDialog.getOpenFileName(
parent, _("Open"), basedir, io.iohandler.get_filters("load", dtype=dtype)
)
sys.stdin, sys.stdout, sys.stderr = saved_in, saved_out, saved_err
filename = str(filename)
try:
data = io.imread(filename, to_grayscale=to_grayscale)
except Exception as msg:
import traceback
traceback.print_exc()
message = _("{filename} could not be opened:\n{msg}").format(
filename=osp.basename(filename), msg=msg
)
QW.QMessageBox.critical(
parent, _("Error") if app_name is None else app_name, message
)
return
return filename, data
def exec_images_open_dialog(
parent: QWidget,
basedir: str = "",
app_name: str | None = None,
to_grayscale: bool = True,
dtype: np.dtype | None = None,
) -> list[tuple[str, np.ndarray]] | None:
"""Executes an image*s* open dialog box (QFileDialog.getOpenFileNames)
Args:
parent: parent widget (None means no parent)
basedir: base directory ('' means current directory)
app_name: application name (used as a title for an eventual
error message box in case something goes wrong when saving image)
to_grayscale (bool | None): convert image to grayscale
dtype: data type
Returns:
(filename, data) tuples if dialog is accepted, None otherwise
"""
saved_in, saved_out, saved_err = sys.stdin, sys.stdout, sys.stderr
sys.stdout = None
filenames, _filter = QW.QFileDialog.getOpenFileNames(
parent, _("Open"), basedir, io.iohandler.get_filters("load", dtype=dtype)
)
sys.stdin, sys.stdout, sys.stderr = saved_in, saved_out, saved_err
filenames = [str(fname) for fname in list(filenames)]
for filename in filenames:
try:
data = io.imread(filename, to_grayscale=to_grayscale)
except Exception as msg:
import traceback
traceback.print_exc()
message = _("{filename} could not be opened:\n{msg}").format(
filename=osp.basename(filename), msg=msg
)
QW.QMessageBox.critical(
parent, _("Error") if app_name is None else app_name, message
)
return
yield filename, data