File: tifimage.py

package info (click to toggle)
python-fabio 0.8.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,116 kB
  • sloc: python: 15,111; ansic: 1,091; sh: 233; makefile: 200
file content (231 lines) | stat: -rw-r--r-- 7,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# coding: utf-8
#
#    Project: FabIO X-ray image reader
#
#    Copyright (C) 2010-2016 European Synchrotron Radiation Facility
#                       Grenoble, France
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

"""
FabIO class for dealing with TIFF images.
In facts wraps TiffIO from V. Armando Solé (available in PyMca) or falls back to PIL

Authors:
........
* Henning O. Sorensen & Erik Knudsen:
  Center for Fundamental Research: Metal Structures in Four Dimensions;
  Risoe National Laboratory;
  Frederiksborgvej 399;
  DK-4000 Roskilde;
  email:erik.knudsen@risoe.dk
* Jérôme Kieffer:
  European Synchrotron Radiation Facility;
  Grenoble (France)

License: MIT
"""
# Get ready for python3:
from __future__ import with_statement, print_function, division

__authors__ = ["Jérôme Kieffer", "Henning O. Sorensen", "Erik Knudsen"]
__date__ = "22/10/2018"
__license__ = "MIT"
__copyright__ = "ESRF, Grenoble & Risoe National Laboratory"
__status__ = "stable"

import time
import logging
logger = logging.getLogger(__name__)

try:
    import PIL
except ImportError:
    PIL = None

import numpy
from .utils import pilutils
from .fabioimage import FabioImage
from . import TiffIO


_USE_TIFFIO = True
"""Uses TiffIO library if available"""

_USE_PIL = True
"""Uses PIL library if available"""


class TiffFrame(object):
    """Frame container for TIFF format"""

    def __init__(self, data, tiff_header):
        self.tiff_header = tiff_header
        self.data = data

    @property
    def header(self):
        """Default header exposed by fabio"""
        return self.tiff_header


class TifImage(FabioImage):
    """
    Images in TIF format
    Wraps TiffIO
    """
    DESCRIPTION = "Tagged image file format"

    DEFAULT_EXTENSIONS = ["tif", "tiff"]

    _need_a_seek_to_read = True

    def __init__(self, *args, **kwds):
        """ Tifimage constructor adds an nbits member attribute """
        self.nbits = None
        FabioImage.__init__(self, *args, **kwds)
        self._tiffio = None
        self.lib = None

    def _readheader(self, infile):
        """
        Try to read Tiff images header...
        """
        header = numpy.frombuffer(infile.read(64), numpy.uint16)
        self.dim1 = int(header[9])
        self.dim2 = int(header[15])
        # nbits is not a FabioImage attribute...
        self.nbits = int(header[21])  # number of bits

    def _create_frame(self, image_data, tiff_header):
        """Create exposed data from TIFF information"""
        return TiffFrame(image_data, tiff_header)

    def _read_header_from_pil(self, image):
        header = self.check_header()
        for num, name in TiffIO.TAG_ID.items():
            if num in image.tag:
                name = name[0].lower() + name[1:]
                value = image.tag[num]
                # For some PIL version the tag content is a tuple
                if isinstance(value, tuple) and len(value) == 1:
                    value = value[0]
                header[name] = value

        return header

    def _read_with_tiffio(self, infile):
        tiffIO = TiffIO.TiffIO(infile)
        self.nframes = tiffIO.getNumberOfImages()
        if tiffIO.getNumberOfImages() > 0:
            # No support for now of multi-frame tiff images
            header = tiffIO.getInfo(0)
            data = tiffIO.getData(0)
            frame = self._create_frame(data, header)
            self.header = frame.header
            self.data = frame.data
        self._tiffio = tiffIO
        if self.data.ndim == 2:
            self.dim2, self.dim1 = self.data.shape
        elif self.data.ndim == 3:
            self.dim2, self.dim1, _ = self.data.shape
            logger.warning("Third dimension is the color")
        else:
            logger.warning("dataset has %s dimensions (%s), check for errors !!!!", self.data.ndim, self.data.shape)
        self.lib = "TiffIO"

    def _read_with_pil(self, infile):
        self.pilimage = PIL.Image.open(infile)
        header = self._read_header_from_pil(self.pilimage)
        data = pilutils.get_numpy_array(self.pilimage)
        frame = self._create_frame(data, header)
        self.header = frame.header
        self.data = frame.data
        self.lib = "PIL"

    def read(self, fname, frame=None):
        """
        Wrapper for TiffIO.
        """
        infile = self._open(fname, "rb")
        self._readheader(infile)
        self.lib = None
        infile.seek(0)

        if _USE_TIFFIO:
            try:
                self._read_with_tiffio(infile)
            except Exception as error:
                logger.warning("Unable to read %s with TiffIO due to %s, trying PIL" % (fname, error))
                logger.debug("Backtrace", exc_info=True)
                infile.seek(0)

        if self.lib is None:
            if _USE_PIL and PIL is not None:
                try:
                    self._read_with_pil(infile)
                except Exception:
                    logger.error("Error in opening %s with PIL" % fname)
                    logger.debug("Backtrace", exc_info=True)
                    if infile.closed:
                        infile = self._open(fname, "rb")
                    else:
                        infile.close()

        if self.lib is None:
            logger.error("Error in opening %s: no tiff reader managed to read the file.", fname)

        self.resetvals()
        return self

    def write(self, fname):
        """
        Overrides the FabioImage.write method and provides a simple TIFF image writer.

        :param str fname: name of the file to save the image to
        """
        with TiffIO.TiffIO(fname, mode="w") as tiff_file:
            tiff_file.writeImage(self.data,
                                 info=self.header,
                                 software="fabio.tifimage",
                                 date=time.ctime())

    def close(self):
        if self._tiffio is not None:
            self._tiffio.close()
            self._tiffio = None
        super(TifImage, self).close()

    def getframe(self, num):
        """Returns the frame `num`.

        This frame is not cached on the image structure.
        """
        if self._tiffio is None:
            raise NotImplementedError("getframe is only implemented for TiffIO lib")

        if 0 <= num < self.nframes:
            image_data = self._tiffio.getData(num)
            tiff_header = self._tiffio.getInfo(num)
            return self._create_frame(image_data, tiff_header)
        raise Exception("getframe out of range")


tifimage = TifImage