[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 |
---|
[872785f] | 18 | from DataLoader.data_info import Image2D |
---|
[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 |
---|
[ded92e4] | 34 | ext = ['.tif','.TIF', |
---|
| 35 | '.jpg','.JPG', |
---|
| 36 | '.png','.PNG', |
---|
| 37 | '.jpeg','.JPEG', |
---|
| 38 | '.tiff','.TIFF', |
---|
| 39 | '.gif','.GIF', |
---|
| 40 | '.bmp', '.BMP'] |
---|
[11a0319] | 41 | |
---|
| 42 | def read(self, filename=None): |
---|
| 43 | """ |
---|
| 44 | Open and read the data in a file |
---|
| 45 | @param file: path of the file |
---|
| 46 | """ |
---|
[daa56d0] | 47 | try: |
---|
[11a0319] | 48 | import Image |
---|
[daa56d0] | 49 | except: |
---|
| 50 | raise RuntimeError, "tiff_reader: could not load file. Missing Image module." |
---|
[11a0319] | 51 | |
---|
[daa56d0] | 52 | # Instantiate data object |
---|
[bdd71f4] | 53 | output = Image2D() |
---|
[daa56d0] | 54 | output.filename = os.path.basename(filename) |
---|
[11a0319] | 55 | |
---|
[daa56d0] | 56 | # Read in the image |
---|
| 57 | try: |
---|
| 58 | im = Image.open(filename) |
---|
| 59 | except : |
---|
| 60 | raise RuntimeError,"cannot open %s"%(filename) |
---|
[bdd71f4] | 61 | data = im.load() |
---|
[872785f] | 62 | |
---|
[bdd71f4] | 63 | x_range = im.size[0] |
---|
| 64 | y_range = im.size[1] |
---|
[daa56d0] | 65 | |
---|
| 66 | # Initiazed the output data object |
---|
[bdd71f4] | 67 | output.image = numpy.zeros([y_range,x_range]) |
---|
| 68 | |
---|
[daa56d0] | 69 | # Initialize |
---|
| 70 | x_vals = [] |
---|
| 71 | y_vals = [] |
---|
| 72 | |
---|
| 73 | # x and y vectors |
---|
[bdd71f4] | 74 | for i_x in range(x_range): |
---|
[daa56d0] | 75 | x_vals.append(i_x) |
---|
[11a0319] | 76 | |
---|
[bdd71f4] | 77 | |
---|
| 78 | for i_y in range(y_range): |
---|
[daa56d0] | 79 | y_vals.append(i_y) |
---|
[bdd71f4] | 80 | |
---|
| 81 | for i_x in range(x_range): |
---|
| 82 | for i_y in range(y_range): |
---|
[8d6440f] | 83 | |
---|
[bdd71f4] | 84 | try: |
---|
| 85 | if data[i_x,i_y].__class__.__name__=="tuple": |
---|
| 86 | |
---|
| 87 | if len(data[i_x,i_y]) == 3: |
---|
| 88 | R,G,B= data[i_x,i_y] |
---|
| 89 | #Converting to L Mode: uses the ITU-R 601-2 luma transform. |
---|
[872785f] | 90 | value = float(R * 299/1000 + G * 587/1000 + B * 114/1000) |
---|
[bdd71f4] | 91 | |
---|
| 92 | elif len(data[i_x,i_y]) == 4: |
---|
| 93 | R,G,B,I = data[i_x,i_y] |
---|
| 94 | #Take only I |
---|
[872785f] | 95 | value = float(R * 299/1000 + G * 587/1000 + B * 114/1000)+float(I) |
---|
[bdd71f4] | 96 | else: |
---|
| 97 | #Take it as Intensity |
---|
[872785f] | 98 | value = float(data[i_x,i_y]) |
---|
[bdd71f4] | 99 | except: |
---|
| 100 | logging.error("tiff_reader: had to skip a non-float point") |
---|
| 101 | continue |
---|
[daa56d0] | 102 | |
---|
[bdd71f4] | 103 | |
---|
| 104 | output.image[y_range-i_y-1,i_x] = value |
---|
| 105 | |
---|
| 106 | output.xbins = x_range |
---|
| 107 | output.ybins = y_range |
---|
[daa56d0] | 108 | output.x_bins = x_vals |
---|
| 109 | output.y_bins = y_vals |
---|
| 110 | output.xmin = 0 |
---|
[bdd71f4] | 111 | output.xmax = x_range |
---|
[daa56d0] | 112 | output.ymin = 0 |
---|
[bdd71f4] | 113 | output.ymax = y_range |
---|
[daa56d0] | 114 | |
---|
| 115 | return output |
---|
[11a0319] | 116 | |
---|
| 117 | |
---|
| 118 | |
---|