浅谈python下tiff图像的读取和保存方法


Posted in Python onDecember 04, 2018

对比测试 scipy.misc PIL.Image libtiff.TIFF 三个库

输入:

1. (读取矩阵) 读入uint8、uint16、float32的lena.tif

2. (生成矩阵) 使用numpy产生随机矩阵,float64的mat

import numpy as np
from scipy import misc
from PIL import Image
from libtiff import TIFF 
#
# 读入已有图像,数据类型和原图像一致
tif32 = misc.imread('.\test\lena32.tif') #<class 'numpy.float32'>
tif16 = misc.imread('.\test\lena16.tif') #<class 'numpy.uint16'>
tif8 = misc.imread('.\test\lena8.tif') #<class 'numpy.uint8'>
# 产生随机矩阵,数据类型float64
np.random.seed(12345)
flt = np.random.randn(512, 512)   #<class 'numpy.float64'>
# 转换float64矩阵type,为后面作测试
z8 = (flt.astype(np.uint8))    #<class 'numpy.uint8'>
z16 = (flt.astype(np.uint16))   #<class 'numpy.uint16'>
z32 = (flt.astype(np.float32))   #<class 'numpy.float32'>

①对读取图像和随机矩阵的存储

# scipy.misc『不论输入数据是何类型,输出图像均为uint8』
misc.imsave('.\test\lena32_scipy.tif', tif32) #--> 8bit(tif16和tif8同)

misc.imsave('.\test\\randmat64_scipy.tif', flt) #--> 8bit
misc.imsave('.\test\\randmat8_scipy.tif', z8) #--> 8bit(z16和z32同)

# PIL.Image『8位16位输出图像与输入数据类型保持一致,64位会存成32位』
Image.fromarray(tif32).save('.\test\lena32_Image.tif') #--> 32bit
Image.fromarray(tif16).save('.\test\lena16_Image.tif') #--> 16bit
Image.fromarray(tif8).save('.\test\lena8_Image.tif') #--> 8bit

Image.fromarray(flt).save('.\test\\randmat_Image.tif') #--> 32bit(flt.min~flt.max)
im = Image.fromarray(flt.astype(np.float32))      
im.save('.\test\\randmat32_Image.tif')     #--> 32bit(灰度值范围同上)
#『uint8和uint16类型转换,会使输出图像灰度变换到255和65535』
im = Image.frombytes('I;16', (512, 512), flt.tostring())
im.save('.\test\\randmat16_Image1.tif')    #--> 16bit(0~65535)
im = Image.fromarray(flt.astype(np.uint16))      
im.save('.\test\\randmat16_Image2.tif')    #--> 16bit(0~65535)
im = Image.fromarray(flt.astype(np.uint8))      
im.save('.\test\\randmat8_Image.tif')     #--> 8bit(0~255)

# libtiff.TIFF『输出图像与输入数据类型保持一致』
tif = TIFF.open('.\test\\randmat_TIFF.tif', mode='w') 
tif.write_image(flt, compression=None)
tif.close() #float64可以存储,但因BitsPerSample=64,一些图像软件不识别
tif = TIFF.open('.\test\\randmat32_TIFF.tif', mode='w') 
tif.write_image(flt.astype(np.float32), compression=None)
tif.close() #--> 32bit(flt.min~flt.max)
#『uint8和uint16类型转换,会使输出图像灰度变换到255和65535』
tif = TIFF.open('.\test\\randmat16_TIFF.tif', mode='w') 
tif.write_image(flt.astype(np.uint16), compression=None)
tif.close() #--> 16bit(0~65535,8位则0~255)

②图像或矩阵归一化对存储的影响

# 『使用scipy,只能存成uint8』
z16Norm = (z16-np.min(z16))/(np.max(z16)-np.min(z16)) #<class 'numpy.float64'>
z32Norm = (z32-np.min(z32))/(np.max(z32)-np.min(z32))
scipy.misc.imsave('.\test\\randmat16_norm_scipy.tif', z16Norm) #--> 8bit(0~255)

# 『使用Image,归一化后变成np.float64 直接转8bit或16bit都会超出阈值,要*255或*65535』
# 『如果没有astype的位数设置,float64会直接存成32bit』
im = Image.fromarray(z16Norm)
im.save('.\test\\randmat16_norm_Image.tif')  #--> 32bit(0~1)
im = Image.fromarray(z16Norm.astype(np.float32))
im.save('.\test\\randmat16_norm_to32_Image.tif') #--> 32bit(灰度范围值同上)
im = Image.fromarray(z16Norm.astype(np.uint16))
im.save('.\test\\randmat16_norm_to16_Image.tif') #--> 16bit(0~1)超出阈值
im = Image.fromarray(z16Norm.astype(np.uint8))
im.save('.\test\\randmat16_norm_to8_Image.tif') #--> 8bit(0~1)超出阈值

im = Image.fromarray((z16Norm*65535).astype(np.uint16))
im.save('.\test\\randmat16_norm_to16_Image1.tif') #--> 16bit(0~65535)
im = Image.fromarray((z16Norm*255).astype(np.uint16))
im.save('.\test\\randmat16_norm_to16_Image2.tif') #--> 16bit(0~255)
im = Image.fromarray((z16Norm*255).astype(np.uint8))
im.save('.\test\\randmat16_norm_to8_Image2.tif') #--> 8bit(0~255)
# 『使用TIFF结果同Image』

③TIFF读取和存储多帧tiff图像

#tiff文件解析成图像序列:读取tiff图像
def tiff_to_read(tiff_image_name): 
 tif = TIFF.open(tiff_image_name, mode = "r") 
 im_stack = list()
 for im in list(tif.iter_images()): 
  im_stack.append(im)
 return 
 #根据文档,应该是这样实现,但测试中不管是tif.read_image还是tif.iter_images读入的矩阵数值都有问题

#图像序列保存成tiff文件:保存tiff图像 
def write_to_tiff(tiff_image_name, im_array, image_num):
 tif = TIFF.open(tiff_image_name, mode = 'w') 
 for i in range(0, image_num): 
  im = Image.fromarray(im_array[i])
  #缩放成统一尺寸 
  im = im.resize((480, 480), Image.ANTIALIAS) 
  tif.write_image(im, compression = None)  
 out_tiff.close() 
 return

补充:libtiff读取多帧tiff图像

因为TIFF.open().read_image()和TIFF.open().iter_images()有问题,则换一种方式读

from libtiff import TIFFfile
tif = TIFFfile('.\test\lena32-3.tif')
samples, _ = tif.get_samples()

以上这篇浅谈python下tiff图像的读取和保存方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用百度翻译进行中翻英示例
Apr 14 Python
Python的Flask框架中web表单的教程
Apr 20 Python
python黑魔法之编码转换
Jan 25 Python
Python3使用正则表达式爬取内涵段子示例
Apr 22 Python
Python实现爬虫设置代理IP和伪装成浏览器的方法分享
May 07 Python
Flask实现跨域请求的处理方法
Sep 27 Python
Empty test suite.(PyCharm程序运行错误的解决方法)
Nov 30 Python
Python 确定多项式拟合/回归的阶数实例
Dec 29 Python
浅谈python常用程序算法
Mar 22 Python
使用selenium和pyquery爬取京东商品列表过程解析
Aug 15 Python
Python列表去重复项的N种方法(实例代码)
May 12 Python
如何用python免费看美剧
Aug 11 Python
对python3新增的byte类型详解
Dec 04 #Python
对Python3中bytes和HexStr之间的转换详解
Dec 04 #Python
python 实现数字字符串左侧补零的方法
Dec 04 #Python
Python发送邮件功能示例【使用QQ邮箱】
Dec 04 #Python
python无限生成不重复(字母,数字,字符)组合的方法
Dec 04 #Python
uwsgi+nginx部署Django项目操作示例
Dec 04 #Python
解决python中无法自动补全代码的问题
Dec 04 #Python
You might like
PHP中运用jQuery的Ajax跨域调用实现代码
2012/02/21 PHP
解决Codeigniter不能上传rar和zip压缩包问题
2014/03/07 PHP
thinkphp自带验证码全面解析
2016/09/18 PHP
ThinkPHP 整合Bootstrap Ajax分页样式
2016/12/23 PHP
PHP文件上传小程序 适合初学者学习!
2019/05/23 PHP
再次更新!MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类 Ver 1.6)
2007/02/05 Javascript
网上抓的一个特效
2007/05/11 Javascript
JavaScript通过正则表达式实现表单验证电话号码
2014/03/07 Javascript
angularjs的一些优化小技巧
2014/12/06 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
在html中引入外部js文件,并调用带参函数的方法
2016/10/31 Javascript
JS定时器用法分析【时钟与菜单中的应用】
2016/12/21 Javascript
JS中关于正则的巧妙操作
2017/08/31 Javascript
angular.js4使用 RxJS 处理多个 Http 请求
2017/09/23 Javascript
快速对接payjq的个人微信支付接口过程解析
2019/08/15 Javascript
vue+ESLint 配置保存 自动格式化代码
2020/03/17 Javascript
[25:59]Newbee vs TNC 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
11个并不被常用但对开发非常有帮助的Python库
2015/03/31 Python
对django中render()与render_to_response()的区别详解
2018/10/16 Python
利用django+wechat-python-sdk 创建微信服务器接入的方法
2019/02/20 Python
pyqt5 QScrollArea设置在自定义侧(任何位置)
2019/09/25 Python
Python API自动化框架总结
2019/11/12 Python
Spartoo比利时:欧洲时尚购物网站
2017/12/06 全球购物
美国眼镜在线零售商:Dualens
2019/12/07 全球购物
年度献血先进个人事迹材料
2014/02/14 职场文书
超市理货员岗位职责
2014/07/04 职场文书
颂军魂爱军营演讲稿
2014/09/13 职场文书
2014年十八届四中全会思想汇报范文
2014/10/17 职场文书
竞聘报告优秀范文
2014/11/06 职场文书
工作期间打牌检讨书范文
2014/11/20 职场文书
市场部岗位职责
2015/02/12 职场文书
穆斯林的葬礼读书笔记
2015/06/26 职场文书
运动会班级前导词
2015/07/20 职场文书
离婚协议书格式范本
2016/03/18 职场文书
实习报告怎么写
2019/06/20 职场文书
vue-router中hash模式与history模式的区别
2021/06/23 Vue.js