Python tifffile.imread() Examples

The following are 30 code examples of tifffile.imread(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tifffile , or try the search function .
Example #1
Source File: util.py    From exposure with MIT License 6 votes vote down vote up
def degrade_images_in_folder(
    folder,
    dst_folder_suffix,
    LIGHTDOWN=True,
    UNBALANCECOLOR=True,):
  import os
  js = os.listdir(folder)
  dst_folder = folder + '-' + dst_folder_suffix
  try:
    os.mkdir(dst_folder)
  except:
    print('dir exist!')
  print('in ' + dst_folder)
  num = 3
  for j in js:
    img = cv2.imread(folder + '/' + j) / 255.
    if LIGHTDOWN:
      for _ in range(num - 1):
        out = pow(img, np.random.uniform(0.4, 0.6)) * np.random.uniform(
            0.25, 0.5)
        cv2.imwrite(dst_folder + '/' + ('L%d-' % _) + j, out * 255.)
      out = img * img
      out = out * (1.0 / out.max())
      cv2.imwrite(dst_folder + '/' + ('L%d-' % num) + j, out * 255.)
    if UNBALANCECOLOR:
      filter = WB2()
      outs = np.array([img] * num)
      features = np.abs(np.random.rand(num, 3))
      for _, out in enumerate(
          filter.process(outs, filter.filter_param_regressor(features))):
        # print out.max()
        out /= out.max()
        out *= np.random.uniform(0.7, 1)
        cv2.imwrite(dst_folder + '/' + ('C%d-' % _) + j, out * 255.) 
Example #2
Source File: util.py    From exposure with MIT License 6 votes vote down vote up
def read_tiff16(fn):
  import tifffile
  import numpy as np
  img = tifffile.imread(fn)
  if img.dtype == np.uint8:
    depth = 8
  elif img.dtype == np.uint16:
    depth = 16
  else:
    print("Warning: unsupported data type {}. Assuming 16-bit.", img.dtype)
    depth = 16

  return (img * (1.0 / (2**depth - 1))).astype(np.float32) 
Example #3
Source File: reggui.py    From suite2p with GNU General Public License v3.0 6 votes vote down vote up
def load_zstack(self):
        name = QtGui.QFileDialog.getOpenFileName(
            self, "Open zstack", filter="*.tif"
        )
        self.fname = name[0]
        try:
            self.zstack = imread(self.fname)
            self.zLy, self.zLx = self.zstack.shape[1:]
            self.Zedit.setValidator(QtGui.QIntValidator(0, self.zstack.shape[0]))
            self.zrange = [np.percentile(self.zstack,1), np.percentile(self.zstack,99)]

            self.computeZ.setEnabled(True)
            self.zloaded = True
            self.zbox.setEnabled(True)
            self.zbox.setChecked(True)
            if 'zcorr' in self.ops[0]:
                if self.zstack.shape[0]==self.ops[0]['zcorr'].shape[0]:
                    zcorr = self.ops[0]['zcorr']
                    self.zmax = np.argmax(gaussian_filter1d(zcorr.T.copy(), 2, axis=1), axis=1)
                    self.plot_zcorr()

        except Exception as e:
            print('ERROR: %s'%e) 
Example #4
Source File: __init__.py    From skylibs with GNU Lesser General Public License v3.0 6 votes vote down vote up
def imread(filename, format_="float32"):
    """Reads an image. Supports exr, hdr, cr2, tiff, jpg, png and
    everything SciPy/PIL supports.

    :filename: file path.
    :format_: format in which to return the value. If set to "native", the
              native format of the file will be given (e.g. uint8 for jpg).
    """
    ldr = False
    _, ext = os.path.splitext(filename.lower())

    if ext == '.exr':
        im = ezexr.imread(filename)
    elif ext in ['.hdr', '.pic']:
        im = _hdr_read(filename)
    elif ext in ['.cr2', '.nef', '.raw']:
        im = _raw_read(filename)
    elif ext in ['.tiff', '.tif']:
        try:
            import tifffile as tiff
        except ImportError:
            print('Install tifffile for better tiff support. Fallbacking to '
                  'scipy.')
            im = scipy_io.imread(filename)
        else:
            im = tiff.imread(filename)
    else:
        im = scipy_io.imread(filename)
        ldr = True

    if format_ == "native":
        return im
    elif ldr and not 'int' in format_:
        return im.astype(format_) / 255.
    else:
        return im.astype(format_) 
Example #5
Source File: test.py    From vnlnet with GNU General Public License v3.0 6 votes vote down vote up
def load_file(f, gray):
    """
    Load a file f.
    gray: whether the image
    should be gray
    """
    if f[-4:] == 'tiff' or f[-3:] == 'tif':
        img = tifffile.imread(f)
    else:
        img = imageio.imread(f)
    if len(img.shape) == 2:
        img = np.expand_dims(img, 2)
    img = np.asarray(img, dtype=np.float32)
    if gray and img.shape[2] > 1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        img = np.expand_dims(img, 2)
    assert(img.shape[2] == 1 or img.shape[2] == 3) # Gray or RGB. Please convert RGBA to RGB.
    img = np.asarray(img, dtype=np.float32)
    img = img/255.

    return img 
Example #6
Source File: io.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def imread(filename: str) -> np.ndarray:
    """Custom implementation of imread to avoid skimage dependency.

    Parameters
    ----------
    filename : string
        The path from which to read the image.

    Returns
    -------
    data : np.ndarray
        The image data.
    """
    filename = abspath_or_url(filename)
    ext = os.path.splitext(filename)[1]
    if ext in [".tif", ".tiff", ".lsm"]:
        import tifffile

        return tifffile.imread(filename)
    else:
        import imageio

        return imageio.imread(filename) 
Example #7
Source File: classify.py    From mlcomp with Apache License 2.0 6 votes vote down vote up
def read_image_file(path: str, gray_scale=False):
        if not os.path.exists(path):
            raise Exception(f'Image at path {path} does not exist')

        if path.endswith('.tiff') and not gray_scale:
            return tifffile.imread(path)
        elif path.endswith('.npy'):
            return np.load(path)
        else:
            if gray_scale:
                img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
                assert img is not None, \
                    f'Image at path {path} is empty'
                return img.astype(np.uint8)
            else:
                img = cv2.imread(path)
                assert img is not None, \
                    f'Image at path {path} is empty'
                return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
Example #8
Source File: utilities.py    From minian with GNU General Public License v3.0 5 votes vote down vote up
def load_images(path, dtype=np.float64):
    # imread = fct.partial(ski.imread, as_gray=True)
    imread = fct.partial(imread_cv, dtype=dtype)
    varr = daim.imread(path, imread)
    varr = xr.DataArray(varr, dims=['frame', 'height', 'width'])
    for dim, length in varr.sizes.items():
        varr = varr.assign_coords(**{dim: np.arange(length)})
    return varr 
Example #9
Source File: train.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def load_image(self, image_id):
        info = self.image_info[image_id]
        if not self.is_uint16:
            image = tifffile.imread(info['path'])
            if self.group=='rgb':
                if image.shape[-1]>3:
                    image=image[:,:,1::]
            image = util.stretch_8bit(image,lower_percent=2,higher_percent=8)

        else:
            image = tifffile.imread(info['path'])

        return image 
Example #10
Source File: dataFunctions.py    From dfc2019 with MIT License 5 votes vote down vote up
def load_img(imgPath):
    """
    Load image
    :param imgPath: path of the image to load
    :return: numpy array of the image
    """
    if imgPath.endswith('.tif'):
        img = tifffile.imread(imgPath)
    else:
        raise ValueError('Install pillow and uncomment line in load_img')
#        img = np.array(Image.open(imgPath))
    return img 
Example #11
Source File: test-mvs.py    From dfc2019 with MIT License 5 votes vote down vote up
def merge_dsm_tifs(folder, count, outname):
    # loop on all input DSM images
    dsm_images = []
    cls_images = []
    for i in range(count):
        name = folder + '{:03d}'.format(i) + '_dsm.tif'
        if os.path.isfile(name):
            next_image = tifffile.imread(name)
            dsm_images.append(next_image)
        name = folder + '{:03d}'.format(i) + '_cls.tif'
        if os.path.isfile(name):
            next_image = tifffile.imread(name)
            cls_images.append(next_image)

    # compute median value for each pixel
    print('Length = ', len(dsm_images))
    dsm_images = np.array(dsm_images)
    cls_images = np.array(cls_images)
    print(dsm_images.shape)
    count = dsm_images.shape[0]
    ydim = dsm_images.shape[1]
    xdim = dsm_images.shape[2]
    median_dsm = np.zeros((ydim, xdim), dtype=np.float32)
    median_cls = np.zeros((ydim, xdim), dtype=np.uint8)
    for i in range(ydim):
        for j in range(xdim):
            pixel = dsm_images[:, i, j]
            pixel = pixel[pixel != NO_DATA]
            count = pixel.shape[0]
            if (count > 0):
                median_dsm[i, j] = np.median(pixel)
            else:
                median_dsm[i, j] = NO_DATA
            pixel = cls_images[:, i, j]
            median_cls[i, j] = get_most_frequent_category(pixel)

    # fill any remaining voids with the max value
    median_dsm[median_dsm == NO_DATA] = median_dsm.max()  # set to max height

    # convert CLS image to LAS conventions
    median_cls = sequential_to_las_labels(median_cls)

    # write median images
    tifffile.imsave(outname + '_DSM.tif', median_dsm)
    tifffile.imsave(outname + '_CLS.tif', median_cls)

    # write median images as uint8 tif for visualization
    median_u8_image = median_dsm - np.min(median_dsm)
    median_u8_image = np.uint8(np.round((median_u8_image / np.max(median_u8_image)) * 255))
    tifffile.imsave(outname + '_stereo_rgb.tif', median_u8_image)
    median_cls_rgb = las_to_sequential_labels(median_cls)
    median_cls_rgb = category_to_color(median_cls_rgb)
    tifffile.imsave(outname + '_segmentation_rgb.tif', median_cls_rgb)

    return median_dsm, median_cls


# main program to demonstrate a baseline MVS algorithm 
Example #12
Source File: update_msi.py    From dfc2019 with MIT License 5 votes vote down vote up
def update_msi(input_file_name, output_file_name):
    img = tifffile.imread(input_file_name)
    rows, cols, bands = img.shape
    driver = gdal.GetDriverByName("GTiff")
    output_data = driver.Create(output_file_name, rows, cols, bands, gdal.GDT_UInt16)
    for band in range(0, bands):
        output_band = output_data.GetRasterBand(band + 1)
        output_band.WriteArray(img[:, :, band])
    output_data.FlushCache()
    output_data = None


# main 
Example #13
Source File: utilities.py    From minian with GNU General Public License v3.0 5 votes vote down vote up
def load_tif_perframe(fname, fid):
    return imread(fname, key=fid) 
Example #14
Source File: prediction.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def get_mpan_image_patches(ms,pan,patch_creator):
    ms,pan =  tifffile.imread(ms), tifffile.imread(pan)
    is_blank = np.sum(pan)==0
    if is_blank:
        return None, None
    if np.argmin(ms.shape) == 2:
        ms = np.transpose(ms, (2, 0, 1))
    img = util.pansharpen(ms, pan)
    img_patches, _, _ = patch_creator.create(img=img)
    return img_patches, img 
Example #15
Source File: utilities.py    From minian with GNU General Public License v3.0 5 votes vote down vote up
def imread_cv(im, dtype=np.float64):
    return (cv2.imread(im, flags=cv2.IMREAD_GRAYSCALE)
            .astype(dtype)) 
Example #16
Source File: utilities.py    From minian with GNU General Public License v3.0 5 votes vote down vote up
def tif_to_varray(filename):
    arr = imread(filename)
    f = arr.shape[0]
    h = arr.shape[1]
    w = arr.shape[2]
    varr = xr.DataArray(
        arr,
        coords=dict(frame=range(f), height=range(h), width=range(w)),
        dims=['frame', 'height', 'width'])
    varr.to_netcdf(os.path.dirname(filename) + os.sep + 'varr_mc_int.nc')
    return varr 
Example #17
Source File: __init__.py    From skylibs with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _raw_read(filename):
    """Calls the dcraw program to unmosaic the raw image."""
    fn, _ = os.path.splitext(filename.lower())
    target_file = "{}.tiff".format(fn)
    if not os.path.exists(target_file):
        ret = subprocess.call('dcraw -v -T -4 -t 0 -j {}'.format(filename))
        if ret != 0:
            raise Exception('Could not execute dcraw. Make sure the executable'
                            ' is available.')
    try:
        import tifffile as tiff
    except ImportError:
        raise Exception('Install tifffile to read the converted tiff file.')
    else:
        return tiff.imread(target_file) 
Example #18
Source File: __init__.py    From skylibs with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _hdr_read(filename, use_imageio=False):
    """Read hdr file.

.. TODO:

    * Support axis other than -Y +X
"""
    if use_imageio:
        return imageio.imread(filename, **kwargs)

    with open(filename, "rb") as f:
        MAGIC = f.readline().strip()
        assert MAGIC == b'#?RADIANCE', "Wrong header found in {}".format(filename)
        comments = b""
        while comments[:6] != b"FORMAT":
            comments = f.readline().strip()
            assert comments[:3] != b"-Y ", "Could not find data format"
        assert comments == b'FORMAT=32-bit_rle_rgbe', "Format not supported"
        while comments[:3] != b"-Y ":
            comments = f.readline().strip()
        _, height, _, width = comments.decode("ascii").split(" ")
        height, width = int(height), int(width)
        rgbe = np.fromfile(f, dtype=np.uint8).reshape((height, width, 4))
        rgb = np.empty((height, width, 3), dtype=np.float)
        rgb[...,0] = np.ldexp(rgbe[...,0], rgbe[...,3].astype('int') - 128)
        rgb[...,1] = np.ldexp(rgbe[...,1], rgbe[...,3].astype('int') - 128)
        rgb[...,2] = np.ldexp(rgbe[...,2], rgbe[...,3].astype('int') - 128)
        # TODO: This will rescale all the values to be in [0, 1]. Find a way to retrieve the original values.
        rgb /= rgb.max()
    return rgb 
Example #19
Source File: input_sixteen.py    From kaggle-satellite-imagery-feature-detection with MIT License 5 votes vote down vote up
def select_and_save(file_names, output_dir):

    # make output directory
    output_dir_path = os.path.join('../../input', output_dir)
    if not os.path.exists(output_dir_path):
        os.makedirs(output_dir_path)

    p = progressbar.ProgressBar(max_value=len(file_names))

    for i, name in enumerate(file_names):

        p.update(i+1)

        image_id = name
        img_3 = np.transpose(tiff.imread("../../dataset/three_band/{}.tif".format(image_id)), (1, 2, 0))
        img_a = np.transpose(tiff.imread("../../dataset/sixteen_band/{}_A.tif".format(image_id)), (1, 2, 0))

        raster_size = img_a.shape
        img_3 = cv2.GaussianBlur(img_3.astype(np.float32), (11, 11), 4, 4)
        img_3 = cv2.resize(img_3, (raster_size[1], raster_size[0]), interpolation=cv2.INTER_CUBIC)
        img_3 = img_3[:, :, [2, 1, 0]]

        img_a_new = _align_two_rasters(img_3, img_a)
        img_a_new *= (2 ** 9)

        output_file_name = name + '.npy'
        np.save(os.path.join(output_dir_path, output_file_name), np.transpose(img_a_new, (2, 0, 1)))

#---------------------------
# main
#---------------------------

# train -------------------- 
Example #20
Source File: prediction.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def get_rgb_image_patches(rgb, patch_creator):
    img = tifffile.imread(rgb)
    is_blank = np.sum(np.sum(img))==0
    if is_blank:
        return None,None
    if np.argmin(img.shape) == 0:
        img = np.transpose(img, (1, 2, 0))
    #img = util.stretch_8bit(img[:, :, [2, 1, 0]],lower_percent=2,higher_percent=98)
    img = img[:,:,[3,2,1,0]]
    img_patches, _ ,_ = patch_creator.create(img=img)
    return img_patches,img 
Example #21
Source File: create_patches_all.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def patches_and_cocoann(gt, outdir_rgb, outdir_mpan, count=1,create_anns=True):
    gt.index = gt.ImageId
    gt_flt = gt[gt.PolygonWKT_Geo != 'POLYGON EMPTY']
    gv = gt_flt.ImageId.value_counts()
    annotations = []
    images = []
    counter = count
    for u in gt_flt.ImageId.unique():
        try:
            pan_sharpen_dir = ''.join([RAW_DIR, gt.name, '/Pan-Sharpen/'])
            image_file = ''.join([pan_sharpen_dir, 'Pan-Sharpen', '_', u, '.tif'])
            img_rgb = tifffile.imread(image_file)
            if np.argmin(img_rgb.shape) == 0:
                img_rgb = np.transpose(img_rgb, (1, 2, 0))
            img_rgb = img_rgb[:, :, [3, 2, 1, 0]]
            img_mpan = get_pan_sharpend(''.join([RAW_DIR, gt.name, '/']), u)

        except:
            print('load error..', u)
            continue
        if gv[u] > 1:
            poly = gt.loc[u].PolygonWKT_Pix.apply(lambda x: loads(x)).values.tolist()
        else:
            poly = [loads(gt.loc[u].PolygonWKT_Pix)]
        mask = util.mask_for_polygons(poly, im_size=imsize)
        img_patches_rgb, mask_patches, kp = patch_creator.create(img=img_rgb, mask=mask, nonzero=True)
        img_patches_mpan, _, _ = patch_creator.create(img=img_mpan, mask=mask, nonzero=True)

        for i, k in enumerate(kp.keys()):
            file_name_rgb = os.path.join(outdir_rgb, ''.join([u, '_', str(k), '.tif']))
            file_name_mpan = os.path.join(outdir_mpan, ''.join([u, '_', str(k), '.tif']))
            if create_anns:
                anns, images_d, counter = create_coco_anns(file_name_rgb, counter, mask_patches[i].squeeze())
                annotations.extend(anns)
                images.extend(images_d)
            tifffile.imsave(file_name_mpan, img_patches_mpan[i].astype('uint16'))
            tifffile.imsave(file_name_rgb, img_patches_rgb[i].astype('uint16'))
        if DEBUG:
            break
    return annotations, images, counter 
Example #22
Source File: create_patches_all.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def get_pan_sharpend(Dir, imageid):
    pan, ms = ''.join([Dir, 'PAN/', 'PAN_', imageid, '.tif']), ''.join([Dir, 'MS/', 'MS_', imageid, '.tif'])
    pan, ms = tifffile.imread(pan), tifffile.imread(ms)
    if np.argmin(ms.shape) == 2:
        ms = np.transpose(ms, (2, 0, 1))
    img = pansharpen(ms, pan)
    return img 
Example #23
Source File: imgutils.py    From spimagine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read3dTiff(fName):
    return imread(fName) 
Example #24
Source File: a04_prepare_data_test.py    From Urban3d with MIT License 5 votes vote down vote up
def convert_tiff_to_png_tst():
    files = glob.glob(INPUT_TESTING_PATH + "/*_RGB.tif")
    print(len(files))
    for f in files:
        id = os.path.basename(f)[:-8]
        print(id)
        dsm_path = os.path.dirname(f) + '/' + id + '_DSM.tif'
        dtm_path = os.path.dirname(f) + '/' + id + '_DTM.tif'
        rgb_path = f
        dsm = tifffile.imread(dsm_path)
        dtm = tifffile.imread(dtm_path)
        rgb = tifffile.imread(rgb_path)

        print('DSM:', dsm.min(), dsm.max())
        print('DTM:', dtm.min(), dtm.max())

        dsm[dsm > -30000] += 220
        dsm[dsm <= -30000] = 0
        dsm *= 160
        # print('DSM:', dsm.min(), dsm.max())
        dsm = dsm.astype(np.uint16)

        dtm[dtm > -30000] += 100
        dtm[dtm <= -30000] = 0
        dtm *= 540
        # print('DTM:', dtm.min(), dtm.max())
        dtm = dtm.astype(np.uint16)

        print('DSM:', dsm.min(), dsm.max())
        print('DTM:', dtm.min(), dtm.max())

        if dsm.shape[:2] != rgb.shape[:2]:
            print('Shape error!', id, dsm.shape[:2], rgb.shape[:2])

        od = OUTPUT_BUILDING_TEST
        dsm_path = od + id + '_dsm.png'
        dtm_path = od + id + '_dtm.png'
        rgb_path = od + id + '_rgb.png'
        cv2.imwrite(dsm_path, dsm)
        cv2.imwrite(dtm_path, dtm)
        cv2.imwrite(rgb_path, rgb) 
Example #25
Source File: benchmark.py    From aicsimageio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _run_benchmark_suite(resources_dir: Path):
    # Default reader / imageio imread tests
    default_reader_single_image_results = _run_benchmark(
        resources_dir=resources_dir,
        extensions=["*.png", "*.jpg", "*.bmp"],
        non_aicsimageio_reader=imageio.imread,
    )

    # Default reader / imageio mimread tests
    default_reader_many_image_results = _run_benchmark(
        resources_dir=resources_dir,
        extensions=["*.gif"],
        non_aicsimageio_reader=imageio.mimread,
    )

    # Tiff reader / tifffile imread tests
    tiff_reader_results = _run_benchmark(
        resources_dir=resources_dir,
        extensions=["*.tiff"],
        non_aicsimageio_reader=tifffile.imread,
    )

    # CZI reader / czifile imread tests
    czi_reader_results = _run_benchmark(
        resources_dir=resources_dir,
        extensions=["*.czi"],
        non_aicsimageio_reader=czifile.imread,
    )

    return [
        *default_reader_single_image_results,
        *default_reader_many_image_results,
        *tiff_reader_results,
        *czi_reader_results,
    ] 
Example #26
Source File: benchmark.py    From aicsimageio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _run_benchmark(
    resources_dir: Path,
    extensions: List[str],
    non_aicsimageio_reader: List[Callable],
    iterations: int = 3,
):
    # Collect files matching the extensions provided
    files = []
    for ext in extensions:
        files += list(resources_dir.glob(ext))

    # Run reads for each file and store details in results
    results = []
    for file in files:
        info_read = aicsimageio.AICSImage(file)
        yx_planes = np.prod(info_read.size("STCZ"))
        for reader in [aicsimageio.imread, non_aicsimageio_reader]:
            reader_path = f"{reader.__module__}.{reader.__name__}"
            for i in tqdm(range(iterations), desc=f"{reader_path}: {file.name}"):
                start = time.perf_counter()
                reader(str(file))
                results.append(
                    {
                        "file_name": file.name,
                        "file_size_gb": file.stat().st_size / 10e8,
                        "reader": (
                            "aicsimageio" if "aicsimageio" in reader_path else "other"
                        ),
                        "yx_planes": int(yx_planes),
                        "read_duration": time.perf_counter() - start,
                    }
                )

    return results 
Example #27
Source File: run_tifffile.py    From recipy with Apache License 2.0 5 votes vote down vote up
def imread(self):
        """
        Use tifffile.imread to read image.tiff.
        """
        file_name = os.path.join(self.data_dir, "image.tiff")
        tifffile.imread(file_name) 
Example #28
Source File: decoding.py    From sentinelhub-py with MIT License 5 votes vote down vote up
def decode_image(data, image_type):
    """ Decodes the image provided in various formats, i.e. png, 16-bit float tiff, 32-bit float tiff, jp2
    and returns it as an numpy array

    :param data: image in its original format
    :type data: any of possible image types
    :param image_type: expected image format
    :type image_type: constants.MimeType
    :return: image as numpy array
    :rtype: numpy array
    :raises: ImageDecodingError
    """
    bytes_data = BytesIO(data)
    if image_type.is_tiff_format():
        image = tiff.imread(bytes_data)
    else:
        image = np.array(Image.open(bytes_data))

        if image_type is MimeType.JP2:
            try:
                bit_depth = get_jp2_bit_depth(bytes_data)
                image = fix_jp2_image(image, bit_depth)
            except ValueError:
                pass

    if image is None:
        raise ImageDecodingError('Unable to decode image')
    return image 
Example #29
Source File: io_utils.py    From sentinelhub-py with MIT License 5 votes vote down vote up
def read_tiff_image(filename):
    """ Read data from TIFF file

    :param filename: name of TIFF file to be read
    :type filename: str
    :return: data stored in TIFF file
    """
    return tiff.imread(filename) 
Example #30
Source File: util.py    From exposure with MIT License 5 votes vote down vote up
def read_tiff_16bit_img_into_XYZ(tiff_fn, exposure=0):
  pp_rgb = tiff.imread(tiff_fn)
  pp_rgb = np.float64(pp_rgb) / (2**16 - 1.0)
  if not pp_rgb.shape[2] == 3:
    print('pp_rgb shape', pp_rgb.shape)
    raise UtilImageError('image channel number is not 3')
  pp_rgb = linearize_ProPhotoRGB(pp_rgb)
  pp_rgb *= np.power(2, exposure)
  xyz = ProPhotoRGB2XYZ(pp_rgb)
  xyz = XYZ_chromatic_adapt(xyz, src_white='D50', dest_white='D65')
  return xyz