1 | """ |
---|
2 | Image reader. Untested. |
---|
3 | """ |
---|
4 | |
---|
5 | """ |
---|
6 | This software was developed by the University of Tennessee as part of the |
---|
7 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
8 | project funded by the US National Science Foundation. |
---|
9 | |
---|
10 | See the license text in license.txt |
---|
11 | |
---|
12 | copyright 2008, University of Tennessee |
---|
13 | """ |
---|
14 | #TODO: load and check data and orientation of the image (needs rendering) |
---|
15 | |
---|
16 | import math, logging, os |
---|
17 | import numpy |
---|
18 | from DataLoader.data_info import Data2D |
---|
19 | |
---|
20 | class Reader: |
---|
21 | """ |
---|
22 | Example data manipulation |
---|
23 | """ |
---|
24 | ## File type |
---|
25 | type_name = "TIF" |
---|
26 | ## Wildcards |
---|
27 | type = ["TIF files (*.tif)|*.tif", |
---|
28 | "TIFF files (*.tiff)|*.tiff", |
---|
29 | ] |
---|
30 | ## Extension |
---|
31 | ext = ['.tif', '.tiff'] |
---|
32 | |
---|
33 | def read(self, filename=None): |
---|
34 | """ |
---|
35 | Open and read the data in a file |
---|
36 | @param file: path of the file |
---|
37 | """ |
---|
38 | try: |
---|
39 | import Image |
---|
40 | except: |
---|
41 | raise RuntimeError, "tiff_reader: could not load file. Missing Image module." |
---|
42 | |
---|
43 | # Instantiate data object |
---|
44 | output = Data2D() |
---|
45 | output.filename = os.path.basename(filename) |
---|
46 | |
---|
47 | # Read in the image |
---|
48 | try: |
---|
49 | im = Image.open(filename) |
---|
50 | except : |
---|
51 | raise RuntimeError,"cannot open %s"%(filename) |
---|
52 | data = im.getdata() |
---|
53 | |
---|
54 | # Initiazed the output data object |
---|
55 | output.data = numpy.zeros([im.size[0],im.size[1]]) |
---|
56 | output.err_data = numpy.zeros([im.size[0],im.size[1]]) |
---|
57 | |
---|
58 | # Initialize |
---|
59 | x_vals = [] |
---|
60 | y_vals = [] |
---|
61 | |
---|
62 | # x and y vectors |
---|
63 | for i_x in range(im.size[0]): |
---|
64 | x_vals.append(i_x) |
---|
65 | |
---|
66 | itot = 0 |
---|
67 | for i_y in range(im.size[1]): |
---|
68 | y_vals.append(i_y) |
---|
69 | |
---|
70 | for val in data: |
---|
71 | try: |
---|
72 | value = float(val) |
---|
73 | except: |
---|
74 | logging.error("tiff_reader: had to skip a non-float point") |
---|
75 | continue |
---|
76 | |
---|
77 | # Get bin number |
---|
78 | if math.fmod(itot, im.size[0])==0: |
---|
79 | i_x = 0 |
---|
80 | i_y += 1 |
---|
81 | else: |
---|
82 | i_x += 1 |
---|
83 | |
---|
84 | output.data[im.size[1]-1-i_y][i_x] = value |
---|
85 | |
---|
86 | itot += 1 |
---|
87 | |
---|
88 | output.xbins = im.size[0] |
---|
89 | output.ybins = im.size[1] |
---|
90 | output.x_bins = x_vals |
---|
91 | output.y_bins = y_vals |
---|
92 | output.xmin = 0 |
---|
93 | output.xmax = im.size[0]-1 |
---|
94 | output.ymin = 0 |
---|
95 | output.ymax = im.size[0]-1 |
---|
96 | |
---|
97 | return output |
---|
98 | |
---|
99 | |
---|
100 | if __name__ == "__main__": |
---|
101 | reader = Reader() |
---|
102 | print reader.read("../test/MP_New.sans") |
---|
103 | |
---|
104 | |
---|