python+tifffile之tiff文件读写方式


Posted in Python onJanuary 13, 2020

背景

使用python操作一批同样分辨率的图片,合并为tiff格式的文件。

由于opencv主要用于读取单帧的tiff文件,对多帧的文件支持并不好。

通过搜索发现了两个比较有用的包:TiffCapture和tifffile。两者都可用pip安装。

其中前者主要用于读取tiff文件,后者可读可写。最终选择tifffile来合成tiff图片文件。

安装tifffile

pip install tifffile

原理及代码

我的图片是8 bit灰度图。

每次读取之后,先升维:

new_gray = gray_img[np.newaxis, ::]

然后再使用np.append添加到数组里。每append一次,相当于tiff增加一帧图片。

tiff_list = np.append(tiff_list, new_gray, axis=0)

所有操作完毕,则一次性保存到磁盘。

tifffile.imsave( out_tiff_path, tiff_list )

下面是我的完整代码:

import cv2
import tifffile
import time
import numpy as np
import time
import os

img_path = '../word_all'
out_txt_path = '../out_word_all.box'
out_tiff_path = '../out_word_all.tif'

tiff_list = None


with open(out_txt_path, 'wb') as f:
  dir_list = os.listdir(img_path)
  cnt_num = 0
  
  for dir_name in dir_list:
    dir_path = os.path.join(img_path, dir_name)
    img_list = os.listdir(dir_path)
    pwd = os.getcwd()
    os.chdir(dir_path)
    
    for img in img_list:
      
      print('dir_path:{}'.format(dir_path))
      gray_img = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
      new_gray = gray_img[np.newaxis, ::]
      print('gray_img shape:{}, new_gray shape:{}'.format(gray_img.shape, new_gray.shape))
      #global cnt_num
      if cnt_num == 0:
        print('cnt_num == 0')
        tiff_list = new_gray
      else:
        print('np.append')
        tiff_list = np.append(tiff_list, new_gray, axis=0)
        print('tiff_list shape:{}'.format(tiff_list.shape))
      
      content = '{} 2 2 60 60 {}\n'.format(dir_name, cnt_num)
      print(content)
      f.write(content.encode('UTF-8'))
      cnt_num += 1
    os.chdir(pwd)

  tifffile.imsave( out_tiff_path, tiff_list )


print('tiff_list shape:{}'.format(tiff_list.shape))

以上这篇python+tifffile之tiff文件读写方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python文件和目录操作详解
Feb 08 Python
Python实时获取cmd的输出
Dec 13 Python
python模拟登录并且保持cookie的方法详解
Apr 04 Python
python的re正则表达式实例代码
Jan 24 Python
python中csv文件的若干读写方法小结
Jul 04 Python
对python3 sort sorted 函数的应用详解
Jun 27 Python
django获取from表单multiple-select的value和id的方法
Jul 19 Python
python使用ctypes调用扩展模块的实例方法
Jan 28 Python
用什么库写 Python 命令行程序(示例代码详解)
Feb 20 Python
如何写python的配置文件
Jun 07 Python
python turtle绘制多边形和跳跃和改变速度特效
Mar 16 Python
Python爬取奶茶店数据分析哪家最好喝以及性价比
Sep 23 Python
python读取tif图片时保留其16bit的编码格式实例
Jan 13 #Python
手动安装python3.6的操作过程详解
Jan 13 #Python
Python中join()函数多种操作代码实例
Jan 13 #Python
Python使用py2neo操作图数据库neo4j的方法详解
Jan 13 #Python
Python模块_PyLibTiff读取tif文件的实例
Jan 13 #Python
python多线程实现代码(模拟银行服务操作流程)
Jan 13 #Python
Python timeit模块的使用实践
Jan 13 #Python
You might like
怎样去阅读一份php源代码
2009/08/21 PHP
深入理解PHP中的Session和Cookie
2013/06/21 PHP
Yii操作数据库实现动态获取表名的方法
2016/03/29 PHP
基于PHP实现用户登录注册功能的详细教程
2020/08/04 PHP
jQuery JSON的解析方式分享
2011/04/05 Javascript
Javascript中查找不以XX字符结尾的单词示例代码
2013/10/15 Javascript
再JavaScript的jQuery库中编写动画效果的指南
2015/08/13 Javascript
Angular.js回顾ng-app和ng-model使用技巧
2016/04/26 Javascript
原生js仿淘宝网商品放大镜效果
2017/02/28 Javascript
微信小程序 图片宽高自适应详解
2017/05/11 Javascript
vue 实现axios拦截、页面跳转和token 验证
2018/07/17 Javascript
如何在node环境实现“get数据解析”代码实例
2020/07/03 Javascript
[43:47]完美世界DOTA2联赛PWL S3 LBZS vs Phoenix 第一场 12.09
2020/12/11 DOTA
python结合opencv实现人脸检测与跟踪
2015/06/08 Python
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
2017/06/21 Python
深入了解Python中pop和remove的使用方法
2018/01/09 Python
python获取当前目录路径和上级路径的实例
2018/04/26 Python
解决Python运行文件出现out of memory框的问题
2018/12/03 Python
python-tornado的接口用swagger进行包装的实例
2019/08/29 Python
python实现将视频按帧读取到自定义目录
2019/12/10 Python
django ListView的使用 ListView中获取url中的参数值方式
2020/03/27 Python
python 制作python包,封装成可用模块教程
2020/07/13 Python
Python爬虫之Selenium实现窗口截图
2020/12/04 Python
小狗电器官方商城:中国高端吸尘器品牌
2017/03/29 全球购物
意大利男装网店:Vrients
2019/05/02 全球购物
俄罗斯宠物用品网上商店:ZooMag
2019/12/12 全球购物
入党积极分子介绍信
2014/01/17 职场文书
11月升旗仪式讲话稿
2014/02/15 职场文书
租房协议书怎么写
2014/04/10 职场文书
副处级干部考察材料
2014/05/17 职场文书
个人批评与自我批评发言稿
2014/09/28 职场文书
2019年怎样写好导游词?
2019/07/02 职场文书
压缩Redis里的字符串大对象操作
2021/06/23 Redis
浅谈哪个Python库才最适合做数据可视化
2021/06/28 Python
从零开始在Centos7上部署SpringBoot项目
2022/04/07 Servers
vue-cil之axios的二次封装与proxy反向代理使用说明
2022/04/07 Vue.js