[daa56d0] | 1 | """ |
---|
| 2 | Image reader. Untested. |
---|
| 3 | """ |
---|
[11a0319] | 4 | |
---|
[daa56d0] | 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. |
---|
[16d8e5f] | 9 | |
---|
[daa56d0] | 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 |
---|
[11a0319] | 19 | |
---|
[bb03739] | 20 | class Reader: |
---|
[11a0319] | 21 | """ |
---|
| 22 | Example data manipulation |
---|
| 23 | """ |
---|
| 24 | ## File type |
---|
[daa56d0] | 25 | type = ["TIF files (*.tif)|*.tif", |
---|
| 26 | "JPG files (*.jpg)|*.jpg", |
---|
| 27 | "JPEG files (*.jpeg)|*.jpeg", |
---|
| 28 | "PNG files (*.png)|*.png", |
---|
| 29 | "TIFF files (*.tiff)|*.tiff", |
---|
| 30 | "BMP files (*.bmp)|*.bmp", |
---|
| 31 | "GIF files (*.gif)|*.gif", |
---|
| 32 | ] |
---|
[11a0319] | 33 | ## Extension |
---|
[daa56d0] | 34 | ext = ['.tif', '.jpg', '.png', '.jpeg', '.tiff', '.gif', '.bmp'] |
---|
[11a0319] | 35 | |
---|
| 36 | def read(self, filename=None): |
---|
| 37 | """ |
---|
| 38 | Open and read the data in a file |
---|
| 39 | @param file: path of the file |
---|
| 40 | """ |
---|
[daa56d0] | 41 | try: |
---|
[11a0319] | 42 | import Image |
---|
[daa56d0] | 43 | except: |
---|
| 44 | raise RuntimeError, "tiff_reader: could not load file. Missing Image module." |
---|
[11a0319] | 45 | |
---|
[daa56d0] | 46 | # Instantiate data object |
---|
| 47 | output = Data2D() |
---|
| 48 | output.filename = os.path.basename(filename) |
---|
[11a0319] | 49 | |
---|
[daa56d0] | 50 | # Read in the image |
---|
| 51 | try: |
---|
| 52 | im = Image.open(filename) |
---|
| 53 | except : |
---|
| 54 | raise RuntimeError,"cannot open %s"%(filename) |
---|
| 55 | data = im.getdata() |
---|
| 56 | |
---|
| 57 | # Initiazed the output data object |
---|
| 58 | output.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) |
---|
[11a0319] | 67 | |
---|
[daa56d0] | 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 |
---|
[11a0319] | 78 | |
---|
[daa56d0] | 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 |
---|
[11a0319] | 85 | |
---|
[daa56d0] | 86 | output.data[im.size[1]-1-i_y][i_x] = value |
---|
[8d6440f] | 87 | |
---|
[daa56d0] | 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 | return output |
---|
[11a0319] | 100 | |
---|
| 101 | |
---|
| 102 | |
---|