本文整理汇总了Python中libtiff.TIFF.open方法的典型用法代码示例。如果您正苦于以下问题:Python TIFF.open方法的具体用法?Python TIFF.open怎么用?Python TIFF.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libtiff.TIFF
的用法示例。
在下文中一共展示了TIFF.open方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _read_data
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def _read_data(path):
''' Read data from given tiff files.
Libtiff is only supported by python 2.7
### Params:
* path - string: path of the data
### Return:
* dataset - ndarray: images or labels
'''
container = np.empty((IMG_SLICES, IMG_SIZE + PAD_SIZE * 2, IMG_SIZE + PAD_SIZE * 2))
# load data
tif = TIFF.open(path)
for i, image in enumerate(tif.iter_images()):
container[i] = np.pad(image, pad_width=((PAD_SIZE,)*2,)*2, mode='symmetric')
tif.close()
return container
# Read data from .tif files and store as ndarray
示例2: split_img
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def split_img():
'''
split a tif volume into single tif
'''
for t in dirtype:
imgdir = TIFF3D.open(t + "-volume.tif")
imgarr = imgdir.read_image()
for i in range(imgarr.shape[0]):
imgname = t + "/" + str(i) + ".tif"
img = TIFF.open(imgname, 'w')
img.write_image(imgarr[i])
示例3: merge_img
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def merge_img():
'''
merge single tif into a tif volume
'''
path = '/home/greg/data/code/python3/UNet/unet/data/'
imgdir = TIFF3D.open("test_mask_volume_server2.tif", 'w')
imgarr = []
for i in range(30):
img = TIFF.open(path + str(i) + ".tif")
imgarr.append(img.read_image())
imgdir.write_image(imgarr)
示例4: loadtiff3d
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def loadtiff3d(filepath):
"""Load a tiff file into 3D numpy array"""
from libtiff import TIFF
tiff = TIFF.open(filepath, mode='r')
stack = []
for sample in tiff.iter_images():
stack.append(np.rot90(np.fliplr(np.flipud(sample))))
out = np.dstack(stack)
tiff.close()
return out
示例5: writetiff3d
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def writetiff3d(filepath, block):
from libtiff import TIFF
try:
os.remove(filepath)
except OSError:
pass
tiff = TIFF.open(filepath, mode='w')
block = np.swapaxes(block, 0, 1)
for z in range(block.shape[2]):
tiff.write_image(np.flipud(block[:, :, z]), compression=None)
tiff.close()
示例6: loadswc
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def loadswc(filepath):
'''
Load swc file as a N X 7 numpy array
'''
swc = []
with open(filepath) as f:
lines = f.read().split("\n")
for l in lines:
if not l.startswith('#'):
cells = l.split(' ')
if len(cells) == 7:
cells = [float(c) for c in cells]
# cells[2:5] = [c-1 for c in cells[2:5]]
swc.append(cells)
return np.array(swc)
示例7: saveswc
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def saveswc(filepath, swc):
if swc.shape[1] > 7:
swc = swc[:, :7]
with open(filepath, 'w') as f:
for i in range(swc.shape[0]):
print('%d %d %.3f %.3f %.3f %.3f %d' %
tuple(swc[i, :].tolist()), file=f)
示例8: test_save_datasets
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def test_save_datasets(self):
"""Test basic writer operation save_datasets."""
import os
import numpy as np
from libtiff import TIFF
from satpy.writers.mitiff import MITIFFWriter
expected = np.full((100, 200), 0)
dataset = self._get_test_datasets()
w = MITIFFWriter(base_dir=self.base_dir)
w.save_datasets(dataset)
filename = (dataset[0].attrs['metadata_requirements']['file_pattern']).format(
start_time=dataset[0].attrs['start_time'])
tif = TIFF.open(os.path.join(self.base_dir, filename))
for image in tif.iter_images():
np.testing.assert_allclose(image, expected, atol=1.e-6, rtol=0)
示例9: test_save_datasets_sensor_set
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def test_save_datasets_sensor_set(self):
"""Test basic writer operation save_datasets."""
import os
import numpy as np
from libtiff import TIFF
from satpy.writers.mitiff import MITIFFWriter
expected = np.full((100, 200), 0)
dataset = self._get_test_datasets_sensor_set()
w = MITIFFWriter(base_dir=self.base_dir)
w.save_datasets(dataset)
filename = (dataset[0].attrs['metadata_requirements']['file_pattern']).format(
start_time=dataset[0].attrs['start_time'])
tif = TIFF.open(os.path.join(self.base_dir, filename))
for image in tif.iter_images():
np.testing.assert_allclose(image, expected, atol=1.e-6, rtol=0)
示例10: test_save_one_dataset
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def test_save_one_dataset(self):
"""Test basic writer operation with one dataset ie. no bands."""
import os
from libtiff import TIFF
from satpy.writers.mitiff import MITIFFWriter
dataset = self._get_test_one_dataset()
w = MITIFFWriter(base_dir=self.base_dir)
w.save_dataset(dataset)
tif = TIFF.open(os.path.join(self.base_dir, os.listdir(self.base_dir)[0]))
IMAGEDESCRIPTION = 270
imgdesc = (tif.GetField(IMAGEDESCRIPTION)).decode('utf-8').split('\n')
for key in imgdesc:
if 'In this file' in key:
self.assertEqual(key, ' Channels: 1 In this file: 1')
示例11: test_save_one_dataset_sesnor_set
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def test_save_one_dataset_sesnor_set(self):
"""Test basic writer operation with one dataset ie. no bands."""
import os
from libtiff import TIFF
from satpy.writers.mitiff import MITIFFWriter
dataset = self._get_test_one_dataset_sensor_set()
w = MITIFFWriter(base_dir=self.base_dir)
w.save_dataset(dataset)
tif = TIFF.open(os.path.join(self.base_dir, os.listdir(self.base_dir)[0]))
IMAGEDESCRIPTION = 270
imgdesc = (tif.GetField(IMAGEDESCRIPTION)).decode('utf-8').split('\n')
for key in imgdesc:
if 'In this file' in key:
self.assertEqual(key, ' Channels: 1 In this file: 1')
示例12: rwlibtiff
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def rwlibtiff(x, fn):
"""
It seems with verion 0.4.0 that it requires Python 2.7, but I get a
segmentation fault even with Python 2.7
"""
from libtiff import TIFFimage, TIFF
with TIFFimage(x, description='my test data') as tf:
print(f'libtiff write {fn}')
# write demo
tf.write_file(str(fn), compression='none')
# read demo
with TIFF.open(str(fn), mode='r') as tif:
return tif.read_image()
# for image in tif.iter_images():
# %% tifffile
if 'tifffile' in modules:
ofn = mkstemp('.tif', 'tifffile')[1]
tic = time()
write_multipage_tiff(imgs, ofn,
descr='0 to 9 numbers',
tags=[(65000, 's', None, 'My custom tag #1', True),
(65001, 's', None, 'My custom tag #2', True),
(65002, 'f', 2, [123456.789, 9876.54321], True)])
y = read_multipage_tiff(ofn)
print(f'{time()-tic:.6f} seconds to read/write {ofn} with tifffile.')
assert (y == imgs).all(), 'tifffile read/write equality failure'
# %%
if 'libtiff' in modules:
tic = time()
ofn = mkstemp('.tif', 'libtiff')[1]
y = rwlibtiff(imgs, ofn)
print(f'{time()-tic:.6f} seconds to read/write {ofn} with libtiff.')
assert (y == imgs).all(), 'libtiff read/write equality failure'
return y
# %% using tifffile
示例13: swc2vtk
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def swc2vtk(swc, outvtkpath):
swc_arr = swc.get_array()
nnode = swc_arr.shape[0]
vtkstr = '# vtk DataFile Version 2.0\n'
vtkstr += 'Generated with Rivuletpy\n'
vtkstr += 'ASCII\n'
vtkstr += 'DATASET POLYDATA\n'
vtkstr += 'POINTS {} float\n'.format(nnode)
id2vtkidx = {}
for i in range(nnode):
vtkstr += '{} {} {}\n'.format(swc_arr[i, 2],
swc_arr[i, 3],
swc_arr[i, 4])
id2vtkidx[int(swc_arr[i, 0])] = i
linectr = 0
vtklinestr = ''
for i in range(nnode):
id, pid = swc_arr[i, 0], swc_arr[i, -1]
if pid >= 0 and int(pid) in id2vtkidx:
linectr += 1
vtklinestr += '{} {} {}\n'.format(2,
id2vtkidx[int(id)],
id2vtkidx[int(pid)])
vtkstr += 'LINES {} {}\n'.format(linectr, linectr * 3)
vtkstr += vtklinestr
vtkstr += "POINT_DATA {}\n".format(nnode)
vtkstr += "SCALARS contourArray double\n"
vtkstr += "LOOKUP_TABLE default\n"
for i in range(nnode):
vtkstr += '{}\n'.format(swc_arr[i, -2])
vtkstr += "SCALARS indicatorArray char\n"
vtkstr += "LOOKUP_TABLE default\n"
for i in range(nnode):
vtkstr += '0\n'
with open(outvtkpath, 'w') as f:
f.write(vtkstr)
示例14: test_save_dataset_with_calibration_one_dataset
# 需要导入模块: from libtiff import TIFF [as 别名]
# 或者: from libtiff.TIFF import open [as 别名]
def test_save_dataset_with_calibration_one_dataset(self):
"""Test saving if mitiff as dataset with only one channel."""
import os
import numpy as np
from libtiff import TIFF
from satpy.writers.mitiff import MITIFFWriter
expected = np.full((100, 200), 255)
expected_key_channel = [u'Table_calibration: BT, BT, °[C], 8, [ 50.00 49.22 48.43 47.65 46.86 46.08 45.29 '
'44.51 43.73 42.94 42.16 41.37 40.59 39.80 39.02 38.24 37.45 36.67 35.88 35.10 34.31 '
'33.53 32.75 31.96 31.18 30.39 29.61 28.82 28.04 27.25 26.47 25.69 24.90 24.12 23.33 '
'22.55 21.76 20.98 20.20 19.41 18.63 17.84 17.06 16.27 15.49 14.71 13.92 13.14 12.35 '
'11.57 10.78 10.00 9.22 8.43 7.65 6.86 6.08 5.29 4.51 3.73 2.94 2.16 1.37 0.59 -0.20 '
'-0.98 -1.76 -2.55 -3.33 -4.12 -4.90 -5.69 -6.47 -7.25 -8.04 -8.82 -9.61 -10.39 -11.18 '
'-11.96 -12.75 -13.53 -14.31 -15.10 -15.88 -16.67 -17.45 -18.24 -19.02 -19.80 -20.59 '
'-21.37 -22.16 -22.94 -23.73 -24.51 -25.29 -26.08 -26.86 -27.65 -28.43 -29.22 -30.00 '
'-30.78 -31.57 -32.35 -33.14 -33.92 -34.71 -35.49 -36.27 -37.06 -37.84 -38.63 -39.41 '
'-40.20 -40.98 -41.76 -42.55 -43.33 -44.12 -44.90 -45.69 -46.47 -47.25 -48.04 -48.82 '
'-49.61 -50.39 -51.18 -51.96 -52.75 -53.53 -54.31 -55.10 -55.88 -56.67 -57.45 -58.24 '
'-59.02 -59.80 -60.59 -61.37 -62.16 -62.94 -63.73 -64.51 -65.29 -66.08 -66.86 -67.65 '
'-68.43 -69.22 -70.00 -70.78 -71.57 -72.35 -73.14 -73.92 -74.71 -75.49 -76.27 -77.06 '
'-77.84 -78.63 -79.41 -80.20 -80.98 -81.76 -82.55 -83.33 -84.12 -84.90 -85.69 -86.47 '
'-87.25 -88.04 -88.82 -89.61 -90.39 -91.18 -91.96 -92.75 -93.53 -94.31 -95.10 -95.88 '
'-96.67 -97.45 -98.24 -99.02 -99.80 -100.59 -101.37 -102.16 -102.94 -103.73 -104.51 '
'-105.29 -106.08 -106.86 -107.65 -108.43 -109.22 -110.00 -110.78 -111.57 -112.35 '
'-113.14 -113.92 -114.71 -115.49 -116.27 -117.06 -117.84 -118.63 -119.41 -120.20 '
'-120.98 -121.76 -122.55 -123.33 -124.12 -124.90 -125.69 -126.47 -127.25 -128.04 '
'-128.82 -129.61 -130.39 -131.18 -131.96 -132.75 -133.53 -134.31 -135.10 -135.88 '
'-136.67 -137.45 -138.24 -139.02 -139.80 -140.59 -141.37 -142.16 -142.94 -143.73 '
'-144.51 -145.29 -146.08 -146.86 -147.65 -148.43 -149.22 -150.00 ]', ]
dataset = self._get_test_dataset_calibration_one_dataset()
w = MITIFFWriter(filename=dataset.attrs['metadata_requirements']['file_pattern'], base_dir=self.base_dir)
w.save_dataset(dataset)
filename = (dataset.attrs['metadata_requirements']['file_pattern']).format(
start_time=dataset.attrs['start_time'])
tif = TIFF.open(os.path.join(self.base_dir, filename))
IMAGEDESCRIPTION = 270
imgdesc = (tif.GetField(IMAGEDESCRIPTION)).decode('utf-8').split('\n')
found_table_calibration = False
number_of_calibrations = 0
for key in imgdesc:
if 'Table_calibration' in key:
found_table_calibration = True
if 'BT' in key:
self.assertEqual(key, expected_key_channel[0])
number_of_calibrations += 1
self.assertTrue(found_table_calibration, "Expected table_calibration is not found in the imagedescription.")
self.assertEqual(number_of_calibrations, 1)
for image in tif.iter_images():
np.testing.assert_allclose(image, expected, atol=1.e-6, rtol=0)