source: sasview/DataLoader/readers/tiff_reader.py @ 872785f

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 872785f was 872785f, checked in by Jae Cho <jhjcho@…>, 15 years ago

working on tiff reader and plotter

  • 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
[872785f]18from DataLoader.data_info import Image2D
[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
[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
[bdd71f4]47        output = Image2D()
[daa56d0]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)
[bdd71f4]55        data = im.load()
[872785f]56
[bdd71f4]57        x_range = im.size[0]
58        y_range = im.size[1]
[daa56d0]59       
60        # Initiazed the output data object
[bdd71f4]61        output.image = numpy.zeros([y_range,x_range])
62
[daa56d0]63        # Initialize
64        x_vals = []
65        y_vals = [] 
66
67        # x and y vectors
[bdd71f4]68        for i_x in range(x_range):
[daa56d0]69            x_vals.append(i_x)
[11a0319]70           
[bdd71f4]71       
72        for i_y in range(y_range):
[daa56d0]73            y_vals.append(i_y)
[bdd71f4]74       
75        for i_x in range(x_range):
76            for i_y in range(y_range):
[8d6440f]77           
[bdd71f4]78                try:
79                    if data[i_x,i_y].__class__.__name__=="tuple":                     
80                       
81                        if len(data[i_x,i_y]) == 3:   
82                            R,G,B= data[i_x,i_y]
83                            #Converting to L Mode: uses the ITU-R 601-2 luma transform.
[872785f]84                            value = float(R * 299/1000 + G * 587/1000 + B * 114/1000) 
[bdd71f4]85                         
86                        elif len(data[i_x,i_y]) == 4:   
87                            R,G,B,I = data[i_x,i_y]
88                            #Take only I
[872785f]89                            value = float(R * 299/1000 + G * 587/1000 + B * 114/1000)+float(I)
[bdd71f4]90                    else:
91                        #Take it as Intensity
[872785f]92                        value = float(data[i_x,i_y])
[bdd71f4]93                except:
94                    logging.error("tiff_reader: had to skip a non-float point")
95                    continue
[daa56d0]96               
[bdd71f4]97                 
98                output.image[y_range-i_y-1,i_x] = value
99       
100        output.xbins      = x_range
101        output.ybins      = y_range
[daa56d0]102        output.x_bins     = x_vals
103        output.y_bins     = y_vals
104        output.xmin       = 0
[bdd71f4]105        output.xmax       = x_range
[daa56d0]106        output.ymin       = 0
[bdd71f4]107        output.ymax       = y_range
[daa56d0]108       
109        return output
[11a0319]110       
111
112
Note: See TracBrowser for help on using the repository browser.