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