source: sasview/DataLoader/readers/tiff_reader.py @ 0f5fe6b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 0f5fe6b was 9d4e502, checked in by Jae Cho <jhjcho@…>, 16 years ago

removed Imag2D class

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[daa56d0]1"""
2    Image reader. Untested.
3"""
[11a0319]4
[daa56d0]5"""
6This software was developed by the University of Tennessee as part of the
7Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
8project funded by the US National Science Foundation.
[16d8e5f]9
[daa56d0]10See the license text in license.txt
11
12copyright 2008, University of Tennessee
13"""
14#TODO: load and check data and orientation of the image (needs rendering)
15
16import math, logging, os
17import numpy
[9d4e502]18from DataLoader.data_info import Data2D
[11a0319]19   
[bb03739]20class 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
[9d4e502]53        output = Data2D()
[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)
[9d4e502]61        predata = 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
[9d4e502]67        output.data = numpy.zeros([y_range,x_range])
68        output.err_data = numpy.zeros([y_range,x_range])
[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:
[9d4e502]85                    if predata[i_x,i_y].__class__.__name__=="tuple":                     
[bdd71f4]86                       
[9d4e502]87                        if len(predata[i_x,i_y]) == 3:   
88                            R,G,B= predata[i_x,i_y]
89                        elif len(predata[i_x,i_y]) == 4:   
90                            R,G,B,I = predata[i_x,i_y]
91                        #Converting to L Mode: uses the ITU-R 601-2 luma transform.
92                        value = float(R * 299/1000 + G * 587/1000 + B * 114/1000) 
[bdd71f4]93                         
94                    else:
95                        #Take it as Intensity
[9d4e502]96                        value = float(predata[i_x,i_y])
[bdd71f4]97                except:
98                    logging.error("tiff_reader: had to skip a non-float point")
99                    continue
[daa56d0]100               
[bdd71f4]101                 
[9d4e502]102                output.data[y_range-i_y-1,i_x] = value
[bdd71f4]103       
104        output.xbins      = x_range
105        output.ybins      = y_range
[daa56d0]106        output.x_bins     = x_vals
107        output.y_bins     = y_vals
108        output.xmin       = 0
[9d4e502]109        output.xmax       = x_range-1
[daa56d0]110        output.ymin       = 0
[9d4e502]111        output.ymax       = y_range-1
[daa56d0]112       
113        return output
[11a0319]114       
Note: See TracBrowser for help on using the repository browser.