def read(self, fname, frame=None): """ Wrapper for TiffIO. """ infile = self._open(fname, "rb") self._readheader(infile) infile.seek(0) self.lib = None try: tiffIO = TiffIO(infile) if tiffIO.getNumberOfImages() > 0: # No support for now of multi-frame tiff images self.data = tiffIO.getImage(0) self.header = tiffIO.getInfo(0) except Exception as error: logger.warning("Unable to read %s with TiffIO due to %s, trying PIL" % (fname, error)) else: if self.data.ndim == 2: self.dim2, self.dim1 = self.data.shape elif self.data.ndim == 3: self.dim2, self.dim1, ncol = 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" if self.lib is None: if Image: try: infile.seek(0) self.pilimage = Image.open(infile) except Exception: logger.error("Error in opening %s with PIL" % fname) self.lib = None infile.seek(0) else: self.lib = "PIL" self.dim1, self.dim2 = self.pilimage.size if self.pilimage.mode in PIL_TO_NUMPY: self.data = numpy.fromstring(self.pilimage.tostring(), PIL_TO_NUMPY[self.pilimage.mode]) else: # probably RGB or RGBA images: rely on PIL to convert it to greyscale float. self.data = numpy.fromstring(self.pilimage.convert("F").tostring(), numpy.float32) self.data.shape = (self.dim2, self.dim1) else: logger.error("Error in opening %s: no tiff reader managed to read the file.", fname) self.lib = None infile.seek(0) self.bpp = len(numpy.zeros(1, dtype=self.data.dtype).tostring()) self.bytecode = self.data.dtype.name self.resetvals() return self
def read(self, fname, frame=None): """ Wrapper for TiffIO. """ infile = self._open(fname, "rb") self._readheader(infile) infile.seek(0) self.lib = None try: tiffIO = TiffIO(infile) if tiffIO.getNumberOfImages() > 0: # No support for now of multi-frame tiff images self.data = tiffIO.getImage(0) self.header = tiffIO.getInfo(0) except Exception as error: logger.warning("Unable to read %s with TiffIO due to %s, trying PIL" % (fname, error)) else: if self.data.ndim == 2: self.dim2, self.dim1 = self.data.shape elif self.data.ndim == 3: self.dim2, self.dim1, ncol = 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" if (self.lib is None): if Image: try: infile.seek(0) self.pilimage = Image.open(infile) except Exception: logger.error("Error in opening %s with PIL" % fname) self.lib = None infile.seek(0) else: self.lib = "PIL" dim1, dim2 = self.pilimage.size if self.pilimage.mode in PIL_TO_NUMPY: dtype = PIL_TO_NUMPY[self.pilimage.mode] pilimage = self.pilimage else: dtype = numpy.float32 pilimage = self.pilimage.convert("F") try: data = numpy.asarray(pilimage, dtype) except: # PIL does not support buffer interface (yet) if "tobytes" in dir(pilimage): data = numpy.fromstring(pilimage.tobytes(), dtype=dtype) else: data = numpy.fromstring(pilimage.tostring(), dtype=dtype) # byteswap ? if numpy.dtype(dtype).itemsize > 1: if (numpy.little_endian and "B" in self.pilimage.mode) or\ (not numpy.little_endian and self.pilimage.mode.endswith("L")): data.byteswap(True) self.data = data.reshape((dim2, dim1)) else: logger.error("Error in opening %s: no tiff reader managed to read the file.", fname) self.lib = None infile.seek(0) self.resetvals() return self