【python图像处理】tiff文件的保存与解析
![]()
tiff文件是一种常用的图像文件格式,支持将多幅图像保存到一个文件中,极大得方便了图像的保存和处理。python中支持tiff文件处理的是libtiff模块中的TIFF类(libtiff下载链接https://pypi.python.org/pypi/libtiff/)。
这里主要介绍tiff文件的解析和保存,具体见如下代码:
- from libtiff import TIFF
- from scipy import misc
-
- def tiff_to_image_array(tiff_image_name, out_folder, out_type):
-
- tif = TIFF.open(tiff_image_name, mode = "r")
- idx = 0
- for im in list(tif.iter_images()):
-
- im_name = out_folder + str(idx) + out_type
- misc.imsave(im_name, im)
- print im_name, 'successfully saved!!!'
- idx = idx + 1
- return
-
- def image_array_to_tiff(image_dir, file_name, image_type, image_num):
-
- out_tiff = TIFF.open(file_name, mode = 'w')
-
-
- for i in range(0, image_num):
- image_name = image_dir + str(i) + image_type
- image_array = Image.open(image_name)
-
- img = image_array.resize((480, 480), Image.ANTIALIAS)
- out_tiff.write_image(img, compression = None, write_rgb = True)
-
- out_tiff.close()
- return