-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageio.py
351 lines (250 loc) · 10.5 KB
/
imageio.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python
"""I/O functions for several image formats.
The relevant formats are TIF, hdf5 and ascii. For ascii and binary numpy formats
no separate functions are provided for saving an image. This is because saving
in these formats requires just a single command:
ascii: np.savetxt('filename', img)
binary (.npy): np.save('filename', img)
"""
import os
import numpy as np
import scipy as sp
# if available, use Zach Pincus' pil_lite which has correct 16-bit TIFF loading
try:
import pil_lite.pil_core.Image as Image
except ImportError:
import Image
#import tables
import tifffile
def list_of_frames(img_name):
"""Return the list of frames for an image file.
Details are as described in the imgimport_intelligent docstring.
"""
img = tifffile.TiffFile(img_name)
#img = Image.open(img_name)
imglist = []
try:
for i in range(np.size(img)):
imdata = img[i].asarray()
imdata = imdata.astype(np.float32)
if np.rank(imdata)==3:
imdata = imdata[:,:,0] + 256*imdata[:,:,1] + 65536*imdata[:,:,2]
imglist.append(imdata)
except EOFError:
pass
if not imglist:
raise ImportError, 'No frames detected in file %s' %img_name
else:
return imglist
def imgimport_intelligent(img_name, foo=3, bar=4, roi_baz=[12,24,56,78]):
"""Opens an image file containing one or more frames
The number of frames in the image is automatically detected. If it is a
single frame, it is assumed to be a transmission image. If there are three
frames, the first one is assumed to be probe with atoms (pwa), the second
one probe without atoms (pwoa) and the third one a dark field (df).
For four frames, it is assumed to be (pwoa, pwa, df1, df2).
For six frames, the first two are discarded (they are for clearing the
CCD charge on the Coolsnap camera), three to six are (pwoa, pwa, df1, df2).
**Inputs**
* img_name: string containing the full path to an image
**Outputs**
* img_array: 3D array, containing the three or four frames of the image,
in the order (pwa, pwoa, df, df2).
**Notes**
The datatype has to be set to float in Winview, otherwise there is a
strange problem reading the frames; support for 16-bit tif files is
lacking a bit in PIL. Note: when pil_lite is available this does work.
The same support is lacking in MS .Net apparently, hence the weird check
for 3-channel TIFFs. What happens here is that XCamera can output multipage
8-bit RGB TIFFs. Each page is of shape (M,N,3), where the 8-bit color
channels combine to output 24-bit B/W data.
"""
imglist = list_of_frames(img_name)
if len(imglist)==1:
return imglist[0]
elif len(imglist) in [3, 4]:
# make an array from the list of frames, with shape (img[0], img[1], 3)
img_array = np.dstack(imglist)
elif len(imglist)==6:
# get rid of first two frames, they're junk. then swap pwoa, pwa.
img_array = np.dstack([imglist[3], imglist[2], imglist[5], imglist[4]])
elif len(imglist)==8:
# get rid of first two frames, then 2x PWA, 2x DF, 2x PWOA (swap DF, PWOA)
img_array = np.dstack([imglist[2], imglist[3], imglist[6], imglist[7],
imglist[4], imglist[5]])
else:
raise ImportError, 'Number of frames is %s' %(len(imglist))
#return img_array
pwa = img_array[:, :, 0]
pwoa = img_array[:, :, 1]
df = img_array[:, :, 2]
nom = pwa - df
den = pwoa - df
nom = np.where(nom<1, 1, nom)
den = np.where(den<1, 1, den)
transimg = nom.astype(float)/den
return img_array
def import_rawframes(img_name):
"""Opens an image file containing three frames
The datatype has to be set to float in Winview, otherwise there is a
strange problem reading the frames; support for 16-bit tif files is
lacking a bit in PIL.
**Inputs**
* img_name: string containing the full path to an image
**Outputs**
* img_array: 3D array, containing the three frames of the image
"""
img = Image.open(img_name)
# note the reversed order because Image and asarray have reversed order
img_array = np.zeros((img.size[1], img.size[0], 3), dtype=np.float32)
img_array[:, :, 0] = np.asarray(img, dtype=np.float32)
try:
img.seek(1) # next frame
img_array[:, :, 1] = np.asarray(img, dtype=np.float32)
img.seek(2) # next frame
img_array[:, :, 2] = np.asarray(img, dtype=np.float32)
except EOFError:
print 'This image contains less than 3 frames'
return None
return img_array
def import_rawimage(img_name):
"""Opens an image file and returns it as an array."""
im = Image.open(img_name)
return np.asarray(im)
def import_xcamera(img_name, ext='xraw'):
"""Load the three .xraw files from XCamera
It is assumed that the file with extension .xraw0 contains the probe
with atoms (pwa), the one with extension .xraw1 the probe without atoms
(pwoa), and the one with extension .xraw2 the dark field (df).
**Inputs**
* img_name: str, name of the image with or without extension
(the extension is stripped and replaced by `ext`.
* ext: str, the extension of the XCamera file. Normally xraw or xroi.
**Outputs**
* raw_array: 3D array, containing the three raw frames (pwa, pwoa, df)
"""
if ext=='xraw':
rawext = ['.xraw0', '.xraw1', '.xraw2']
elif ext=='xroi':
rawext = ['.xroi0', '.xroi1', '.xroi2']
else:
raise ValueError, 'Unknown extension for XCamera file'
basename = os.path.splitext(img_name)[0]
try:
pwa = np.loadtxt(''.join([basename, rawext[0]]), dtype=np.int16)
pwoa = np.loadtxt(''.join([basename, rawext[1]]), dtype=np.int16)
df = np.loadtxt(''.join([basename, rawext[2]]), dtype=np.int16)
except IOError, e:
print e
return None
raw_array = np.dstack([pwa, pwoa, df])
return raw_array
def save_tifimage(imgarray, fname, dirname=None):
"""Save a single image in TIF format
**Inputs**
* imgarray: 2D array, containing a single frame image
* fname: str, filename of the file to save, optionally including
the full path to the directory
* dirname: str, if not None, fname will be appended to dirname to
obtain the full path of the file to save.
**Notes**
Multiple frame tif images are not supported. For such data hdf5 is the
recommended format.
"""
if dirname:
fname = os.path.join(dirname, fname)
fname = ''.join([os.path.splitext(fname)[0], '.tif'])
im = sp.misc.toimage(imgarray, mode='F')
im.save(fname, mode='F')
def save_hdfimage(imgarray, fname, dirname=None):
"""Save an image to an hdf5 file
**Inputs**
* imgarray: ndarray, containing the image data. If the array is 2D,
it is assumed that this is a single frame image. If it is
3D, the frames will be saved as separate arrays:
('pwa', 'pwoa', 'df'), and if there is a fourth frame this
is df2.
* fname: str, filename of the file to save, optionally including
the full path to the directory
* dirname: str, if not None, fname will be appended to dirname to
obtain the full path of the file to save.
"""
if dirname:
fname = os.path.join(dirname, fname)
fname = ''.join([os.path.splitext(fname)[0], '.h5'])
# Open a new empty HDF5 file
h5file = tables.openFile(fname, mode='w')
if len(imgarray.shape)==2:
# Get the root group
root = h5file.root
# Save image in the HDF5 file
h5file.createArray(root, 'img', imgarray, title='Transmission image')
elif len(imgarray.shape)==3:
# image frames to be saved
pwa = imgarray[:, :, 0]
pwoa = imgarray[:, :, 1]
df = imgarray[:, :, 2]
# Get the root group
root = h5file.createGroup("/", 'rawframes', 'The raw image frames')
# Save image in the HDF5 file
h5file.createArray(root, 'pwa', pwa, title='Probe with atoms')
h5file.createArray(root, 'pwoa', pwoa, title='Probe without atoms')
h5file.createArray(root, 'df', df, title='Dark field')
if imgarray.shape[2] > 3:
df2 = imgarray[:, :, 3]
h5file.createArray(root, 'df2', df2, title='Dark field 2')
else:
print 'imgarray does not have the right dimensions, shape is: ', \
imgarray.shape
h5file.close()
def load_hdfimage(fname, dirname=None, ext_replace=True):
"""Load an image from an hdf5 file
**Inputs**
* fname: str, filename of the file to save, optionally including
the full path to the directory
* dirname: str, if not None, fname will be appended to dirname to
obtain the full path of the file to save.
* ext_replace: bool, if True replaces the extension of fname with `.h5`
**Outputs**
* transimg: ndarray, the image data
"""
if dirname:
fname = os.path.join(dirname, fname)
if ext_replace:
fname = ''.join([os.path.splitext(fname)[0], '.h5'])
h5file = tables.openFile(fname, mode='r')
try:
transimg = np.asarray(h5file.root.img)
return transimg
except tables.NoSuchNodeError:
pwa = np.asarray(h5file.root.rawframes.pwa)
pwoa = np.asarray(h5file.root.rawframes.pwoa)
df = np.asarray(h5file.root.rawframes.df)
imgarray = np.dstack([pwa, pwoa, df])
return imgarray
finally:
h5file.close()
def trans_nonorm(img_name):
raw_frames=imgimport_intelligent(img_name)
pwa = raw_frames[:, :, 0]
pwoa = raw_frames[:, :, 1]
df = raw_frames[:, :, 2]
nom = pwa - df
den = pwoa - df
nom = np.where(nom<1, 1, nom)
den = np.where(den<1, 1, den)
transimg = nom.astype(float)/den
return transimg/1.35
def convert_xcamera_to_hdf5(imglist, ext='xraw'):
"""Convert every file in imglist to an hdf5 file.
The raw files are saved in the hdf5 file as
`root.rawframes.pwa`, `root.rawframes.pwoa`, `root.rawframes.df`.
Their dtype is uint16, which results in files of a third smaller than
the xcamera text files.
**Inputs**
* imglist: list of str, paths to .xraw0 files
* ext: str, the extension of the XCamera file. Normally xraw or xroi.
"""
for img in imglist:
imgarray = import_xcamera(img, ext=ext)
save_hdfimage(imgarray, img)