您现在的位置是:首页 > 文章详情

【python图像处理】tiff文件的保存与解析

日期:2018-04-08点击:621

【python图像处理】tiff文件的保存与解析

tiff文件是一种常用的图像文件格式,支持将多幅图像保存到一个文件中,极大得方便了图像的保存和处理。python中支持tiff文件处理的是libtiff模块中的TIFF类(libtiff下载链接https://pypi.python.org/pypi/libtiff/)。

这里主要介绍tiff文件的解析和保存,具体见如下代码:

[python] view plain copy

  1. from libtiff import TIFF
  2. from scipy import misc
  3. ##tiff文件解析成图像序列
  4. ##tiff_image_name: tiff文件名;
  5. ##out_folder:保存图像序列的文件夹
  6. ##out_type:保存图像的类型,如.jpg、.png、.bmp等
  7. def tiff_to_image_array(tiff_image_name, out_folder, out_type):
  8.     tif = TIFF.open(tiff_image_name, mode = "r")
  9.     idx = 0
  10.     for im in list(tif.iter_images()):
  11.         #
  12.         im_name = out_folder + str(idx) + out_type
  13.         misc.imsave(im_name, im)
  14.         print im_name, 'successfully saved!!!'
  15.         idx = idx + 1
  16.     return
  17. ##图像序列保存成tiff文件
  18. ##image_dir:图像序列所在文件夹
  19. ##file_name:要保存的tiff文件名
  20. ##image_type:图像序列的类型
  21. ##image_num:要保存的图像数目
  22. def image_array_to_tiff(image_dir, file_name, image_type, image_num):
  23.     out_tiff = TIFF.open(file_name, mode = 'w')
  24.     #这里假定图像名按序号排列
  25.     for i in range(0, image_num):
  26.         image_name = image_dir + str(i) + image_type
  27.         image_array = Image.open(image_name)
  28.         #缩放成统一尺寸
  29.         img = image_array.resize((480480), Image.ANTIALIAS)
  30.         out_tiff.write_image(img, compression = None, write_rgb = True)
  31.     out_tiff.close()
  32.     return
原文链接:https://yq.aliyun.com/articles/578165
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章