本文整理汇总了Python中libtiff.TIFF类的典型用法代码示例。如果您正苦于以下问题:Python TIFF类的具体用法?Python TIFF怎么用?Python TIFF使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TIFF类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_data
def get_data(self):
filenames = self.get_filesindirectory(self.get_prefix(), self.get_extension())
try:
from libtiff import TIFF
im = TIFF.open(filenames[0])
v0 = im.read_image()
x = NXfield(range(v0.shape[1]), dtype=np.uint16, name="x")
y = NXfield(range(v0.shape[0]), dtype=np.uint16, name="y")
z = NXfield(range(1, len(filenames) + 1), dtype=np.uint16, name="z")
v = NXfield(np.zeros(shape=(len(filenames), v0.shape[0], v0.shape[1]), dtype=v0.dtype), name="v")
v[0] = v0
for i in range(1, len(filenames)):
im = TIFF.open(filenames[i])
v[i] = im.read_image()
except ImportError:
im = Image.open(filenames[0])
dtype = np.dtype(np.uint16)
if im.mode == "I;32" or im.mode == "I":
dtype = np.dtype(np.uint32)
x = NXfield(range(im.size[0]), dtype=np.uint16, name="x")
y = NXfield(range(im.size[1]), dtype=np.uint16, name="y")
z = NXfield(range(1, len(filenames) + 1), dtype=np.uint16, name="z")
v = NXfield(np.zeros(shape=(len(filenames), im.size[1], im.size[0]), dtype=dtype), name="v")
v[0] = np.array(im.getdata(), dtype=dtype).reshape(im.size[1], im.size[0])
for i in range(1, len(filenames)):
im = Image.open(filenames[i])
v[i] = np.array(im.getdata(), dtype=dtype).reshape(im.size[1], im.size[0])
return NXentry(NXdata(v, (z, y, x)))
示例2: convert_tiff_to_mat
def convert_tiff_to_mat(Working_Directory, img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, img_size_crop_y2,\
median_filter_threshold, text_file):
#Get names of all tiff files in the directory
onlyfiles = [ f for f in os.listdir(Working_Directory)\
if (os.path.isfile(os.path.join(Working_Directory, f)) and f.find('.tif')>0 and f.find('T=')>=0)]
Working_Directory2 = Working_Directory.replace('/Thresholded_OB','')
for lst in xrange(1,np.size(onlyfiles, axis=0)+1):
tif1 = TIFF.open(Working_Directory+'T='+str(lst)+'.tif', mode='r') #Open multitiff
tif2 = TIFF.open(Working_Directory2+'T='+str(lst)+'.tif', mode='r') #Open non thresholded image for template
#Initialize data matrix based on number of planes in the multitiff
if lst==1:
count_z = 0
for image in tif1.iter_images():
count_z = count_z + 1
data_filtered = np.zeros((img_size_x-(img_size_crop_x1+img_size_crop_x2), img_size_y-(img_size_crop_y1+img_size_crop_y2), count_z,np.size(onlyfiles,0)), dtype=np.uint8)
data = np.zeros((img_size_x-(img_size_crop_x1+img_size_crop_x2), img_size_y-(img_size_crop_y1+img_size_crop_y2), count_z,np.size(onlyfiles,0)), dtype=np.uint8)
data_filtered, count_z = get_tif_images_filtered(data_filtered, lst, onlyfiles, text_file, tif1, \
img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, \
img_size_crop_y2,median_filter_threshold)
data = get_tif_images_raw(data, lst, onlyfiles, text_file, tif2, \
img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, \
img_size_crop_y2)
return data, data_filtered, count_z
示例3: downsample_tif
def downsample_tif(filename_in, filename_out):
input_tiff = TIFF.open(filename_in, mode = "r")
output_tiff = TIFF.open(filename_out, mode = "w")
counter = 0
for img in input_tiff.iter_images():
img = img[::2, ::2]
output_tiff.write_image(img)
counter += 1
if counter /100 == counter / 100.:
print counter
示例4: convert_tiff_to_mat
def convert_tiff_to_mat(Working_Directory, img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, img_size_crop_y2,\
median_filter_threshold, text_file):
#Get names of all tiff files in the directory
onlyfiles = [ f for f in os.listdir(Working_Directory)\
if (os.path.isfile(os.path.join(Working_Directory, f)) and f.find('.tif')>0 and f.find('T=')>=0)]
Working_Directory2 = Working_Directory
average_bg_intensity = np.zeros((np.size(onlyfiles, axis=0),1))
for lst in xrange(1,np.size(onlyfiles, axis=0)+1):
tif1 = TIFF.open(Working_Directory+'T='+str(lst)+'.tif', mode='r') #Open multitiff
tif2 = TIFF.open(Working_Directory2+'T='+str(lst)+'.tif', mode='r') #Open non thresholded image for template
#Initialize data matrix based on number of planes in the multitiff
if lst==1:
count_z = 0
for image in tif1.iter_images():
count_z = count_z + 1
data_filtered_raw = np.zeros((img_size_x-(img_size_crop_x1+img_size_crop_x2), img_size_y-(img_size_crop_y1+img_size_crop_y2), count_z,np.size(onlyfiles,0)), dtype=np.uint8)
data_filtered_bgsub = np.zeros((img_size_x-(img_size_crop_x1+img_size_crop_x2), img_size_y-(img_size_crop_y1+img_size_crop_y2), count_z,np.size(onlyfiles,0)), dtype=np.uint8)
data = np.zeros((img_size_x-(img_size_crop_x1+img_size_crop_x2), img_size_y-(img_size_crop_y1+img_size_crop_y2), count_z,np.size(onlyfiles,0)), dtype=np.uint8)
data_filtered_raw, data_filtered_bgsub, count_z, average_bg_intensity[lst-1] = get_tif_images_filtered(data_filtered_raw, data_filtered_bgsub, lst, onlyfiles, text_file, tif1, \
img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, \
img_size_crop_y2,median_filter_threshold)
data = get_tif_images_raw(data, lst, onlyfiles, text_file, tif2, \
img_size_x, img_size_y, img_size_crop_x1,img_size_crop_x2, img_size_crop_y1, \
img_size_crop_y2)
average_bg_intensity = np.array(average_bg_intensity)
print np.shape(average_bg_intensity)
plt.plot(average_bg_intensity)
plt.show()
### If intensities are correlated to the Bg intensity, make them zero
for ii in xrange(0,np.size(data_filtered_raw,0)):
for jj in xrange(0,np.size(data_filtered_raw,1)):
for zz in xrange(0, np.size(data_filtered_raw,2)):
A = copy(data_filtered_raw[ii,jj,zz,:])
corr_A = np.corrcoef(A,np.squeeze(average_bg_intensity))
if corr_A[0,1] > 0.6 and sum(A)!=0:
A[:] = 0
data_filtered_bgsub[ii,jj,zz,:] = A
data[ii,jj,zz,:] = A
return data, data_filtered_bgsub, count_z, average_bg_intensity
示例5: load_stack
def load_stack(self):
filename, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open file', self.last_dir, "TIFF (*.tif *.tiff);; All files (*.*)")
if not len(filename):
return
self.init_data()
self.last_dir = os.path.dirname(filename)
self.image_location = filename
tif = TIFF.open(self.image_location)
pic = tif.read_image()
misc.imsave("temp.jpg", pic)
# TODO: wait message
#msg_box = QtGui.QMessageBox.information(self, "CellECT New Workspace", "Loading a large stack may take time. Press OK to continue.", defaultB = QtGui.QMessageBox.NoButton )
self.load_metadata_from_tif_info()
#pic = QtGui.QImage(filename)
#self.label_preview_img.setPixmap(QtGui.QPixmap.fromImage(pic))
self.label_preview_img.setPixmap("temp.jpg")
示例6: test_write_lzw
def test_write_lzw():
for itype in [uint8, uint16, uint32, uint64,
int8, int16, int32, int64,
float32, float64,
complex64, complex128]:
#image = array([[1,2,3], [4,5,6]], itype)
image = array([range(10000)], itype)
#image = array([[0]*14000], itype)
fn = mktemp('.tif')
tif = TIFFimage(image)
tif.write_file(fn, compression='lzw')
del tif
#os.system('wc %s; echo %s' % (fn, image.nbytes))
tif = TIFF.open(fn,'r')
image2 = tif.read_image()
tif.close()
#os.remove(fn)
atexit.register(os.remove, fn)
for i in range(image.size):
if image.flat[i] != image2.flat[i]:
print `i, image.flat[i-5:i+5].view(dtype=uint8),image2.flat[i-5:i+5].view(dtype=uint8)`
break
assert image.dtype==image2.dtype
assert (image==image2).all()
示例7: save_images_to_disk
def save_images_to_disk():
print ("Disk-saving thread active...")
n = 0
frameTimeOutputFile = open(planOutputPath + "frameTimes.txt", "w")
frameTimeOutputFile.write("frameCount\t n\t frameCond\t frameT\t interval\n")
currdict = im_queue.get()
while currdict is not None:
frameTimeOutputFile.write(
"%i\t %i\t %i\t %s\t %s\n"
% (int(currdict["frame"]), n, int(currdict["cond"]), currdict["time"], currdict["interval"])
)
if save_as_tiff:
fname = "%s/frame%i.tiff" % (dataOutputPath, int(currdict["frame"]))
tiff = TIFF.open(fname, mode="w")
tiff.write_image(currdict["im"])
tiff.close()
elif save_as_npz:
np.savez_compressed("%s/test%d.npz" % (output_path, n), currdict["im"])
else:
fname = "%s/frame%i.tiff" % (dataOutputPath, int(currdict["frame"]))
with open(fname, "wb") as f:
pkl.dump(currdict, f, protocol=pkl.HIGHEST_PROTOCOL)
# print 'DONE SAVING FRAME: ', currdict['frame'], n #fdict
n += 1
currdict = im_queue.get()
disk_writer_alive = False
# frameTimeOutputFile.close()
print ("Disk-saving thread inactive...")
示例8: _write_single_image
def _write_single_image(full_uri, image_array):
"""
internally used method to write single tiff image
"""
tiff = TIFF.open(full_uri, mode='w')
tiff.write_image(image_array.astype('uint16'), write_rgb=False)
tiff.close()
示例9: load_info_from_tif
def load_info_from_tif(self, filename):
tif = TIFF.open(filename)
buf = StringIO. StringIO(tif.info())
img = tif.read_image()
for image in tif.iter_images():
self.num_pages += 1
print self.num_pages
self.numx = img.shape[0]
self.numy = img.shape[1]
line = buf.readline()
self.memch = 0
while line:
self.get_meta_from_line(line)
line = buf.readline()
if self.numt ==0:
self.numt = 1
if self.numz ==0:
self.numz =1
if self.numch == 0:
self.numch = 1
示例10: test_write_read
def test_write_read():
for compression in [None, 'lzw']:
for itype in [uint8, uint16, uint32, uint64,
int8, int16, int32, int64,
float32, float64,
complex64, complex128]:
image = array([[1,2,3], [4,5,6]], itype)
fn = mktemp('.tif')
if 0:
tif = TIFF.open(fn,'w')
tif.write_image(image, compression=compression)
tif.close()
else:
tif = TIFFimage(image)
tif.write_file(fn, compression=compression)
del tif
tif = TIFFfile(fn)
data, names = tif.get_samples()
assert names==['sample0'],`names`
assert len(data)==1, `len(data)`
assert image.dtype==data[0].dtype, `image.dtype, data[0].dtype`
assert (image==data[0]).all()
#os.remove(fn)
atexit.register(os.remove, fn)
示例11: create_plane
def create_plane(self,roi,sizeX,sizeY,sizeC,description):
tif_image = TIFF.open(self.filename, 'w')
im_dtype = np.dtype('uint8')
image_data = np.zeros((sizeC,sizeY,sizeX),dtype=im_dtype)
print 'num channels=',sizeC
for c in range(sizeC):
if c == 0:
tif_image.set_description(description)
channel = self.channels[c]
imarray = self.mkplane(roi,c)
print 'imarray shape:',imarray.shape
print("Writing channel: ", c+1)
if self.rotation == 0:
plane = imarray[0,:,:]
if self.rotation == 1:
plane = np.rot90(imarray[0,:,:],1)
elif self.rotation == 2:
plane = np.rot90(imarray[0,:,:],3)
image_data[c,:,:] = plane
# tif_image = TIFFimage(image_data,description=description)
# tif_image.write_file(self.filename,compression='lzw')
# del tif_image
tif_image.write_image(image_data, compression=self.compression.encode('ascii','ignore'))
tif_image.close()
示例12: __init__
def __init__(self, filename):
self.filename = filename
self.tiff = TIFF.open(filename)
self._count = self._count_frames() # used once by TiffStack
self._shape = self.tiff.read_image().shape # used once by TiffStack
self.end = False
self.generator = self.tiff.iter_images()
示例13: get_list_of_stimulus_name
def get_list_of_stimulus_name(Working_Directory):
## To find num z planes in each trial directory
num_z_planes = []
Name_stimulus = list()
Stimulus_Directories = [f for f in os.listdir(Working_Directory) if os.path.isdir(os.path.join(Working_Directory, f)) and f.find('Figures')<0]
for ii in xrange(0, size(Stimulus_Directories, axis = 0)):
Trial_Directories = [f for f in os.listdir(os.path.join(Working_Directory, Stimulus_Directories[ii]))\
if os.path.isdir(os.path.join(Working_Directory, Stimulus_Directories[ii], f)) and f.find('Figures')<0] #Get only directories
temp_num_z_planes = zeros((size(Trial_Directories)), dtype=int)
for jj in xrange(0, size(Trial_Directories, axis = 0)):
Image_Directory = os.path.join(Working_Directory, Stimulus_Directories[ii], Trial_Directories[jj], 'C=1')+filesep
tif = TIFF.open(Image_Directory +'T=1.tif', mode='r') #Open multitiff
count = 1
for image in tif.iter_images():
temp_num_z_planes[jj] = count
count = count+1
num_z_planes.append(temp_num_z_planes)
for ii in xrange(0, size(Stimulus_Directories, axis = 0)):
Trial_Directories = [f for f in os.listdir(os.path.join(Working_Directory, Stimulus_Directories[ii]))\
if os.path.isdir(os.path.join(Working_Directory, Stimulus_Directories[ii], f)) and f.find('Figures')<0] #Get only directories
for jj in xrange(0, size(Trial_Directories, axis = 0)):
for kk in xrange(0, num_z_planes[ii][jj]):
name_for_saving_figures = Stimulus_Directories[ii] + ' ' + Trial_Directories[jj] + ' Z=' + str(kk+1)
Name_stimulus.append(name_for_saving_figures)
return Name_stimulus
示例14: save_images_to_disk
def save_images_to_disk():
print('Disk-saving thread active...')
n = 0
frameTimeOutputFile = open(planOutputPath+'frameTimes.txt','w')
frameTimeOutputFile.write('frameCount\t n\t frameCond\t frameT\t interval\n')
currdict = im_queue.get()
while currdict is not None:
frameTimeOutputFile.write('%i\t %i\t %i\t %s\t %s\n' % (int(currdict['frame']),n,int(currdict['cond']),currdict['time'],currdict['interval']))
if save_as_tiff:
fname = '%s/frame%i.tiff' % (dataOutputPath,int(currdict['frame']))
tiff = TIFF.open(fname, mode='w')
tiff.write_image(currdict['im'])
tiff.close()
elif save_as_npz:
np.savez_compressed('%s/test%d.npz' % (output_path, n), currdict['im'])
else:
fname = '%s/frame%i.tiff' % (dataOutputPath,int(currdict['frame']),)
with open(fname, 'wb') as f:
pkl.dump(currdict, f, protocol=pkl.HIGHEST_PROTOCOL)
# print 'DONE SAVING FRAME: ', currdict['frame'], n #fdict
n += 1
currdict = im_queue.get()
disk_writer_alive = False
#frameTimeOutputFile.close()
print('Disk-saving thread inactive...')
示例15: image_to_tiff
def image_to_tiff(src_path, des_path):
from libtiff import TIFF
image = Image.open(src_path)
image = image.convert('L')
tiff_out = TIFF.open(des_path, 'w')
tiff_out.write_image(image, compression=None, write_rgb=True)
tiff_out.close()