浅谈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和Ruby中each循环引用变量问题(一个隐秘BUG?)
Jun 04 Python
python登陆asp网站页面的实现代码
Jan 14 Python
python socket多线程通讯实例分析(聊天室)
Apr 06 Python
Python制作刷网页流量工具
Apr 23 Python
Python爬虫文件下载图文教程
Dec 23 Python
Python基于opencv实现的简单画板功能示例
Mar 04 Python
Python hashlib模块实例使用详解
Dec 24 Python
python实现异常信息堆栈输出到日志文件
Dec 26 Python
利用Python计算KS的实例详解
Mar 03 Python
python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例
Mar 06 Python
Jupyter notebook 远程配置及SSL加密教程
Apr 14 Python
Selenium浏览器自动化如何上传文件
Apr 06 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 代码优化之经典示例
2011/03/24 PHP
php中将地址生成迅雷快车旋风链接的代码[测试通过]
2011/04/20 PHP
Php中使用Select 查询语句的实例
2014/02/19 PHP
[原创]CI(CodeIgniter)简单统计访问人数实现方法
2016/01/19 PHP
php注册和登录界面的实现案例(推荐)
2016/10/24 PHP
JavaScript 学习笔记(九)call和apply方法
2010/01/11 Javascript
jquery实现点击文字可编辑并修改保存至数据库
2014/04/15 Javascript
JS清除文本框内容离开在恢复及鼠标离开文本框时触发js的方法
2016/01/12 Javascript
jquery动态切换背景图片的简单实现方法
2016/05/14 Javascript
原生js验证简洁注册登录页面
2016/12/17 Javascript
理解javascript中的闭包
2017/01/11 Javascript
详解JS中的立即执行函数
2017/02/24 Javascript
js实现二级导航功能
2017/03/03 Javascript
Nodejs回调加超时限制两种实现方法
2017/06/09 NodeJs
vue的过滤器filter实例详解
2018/09/17 Javascript
详解javascript函数写法大全
2019/03/25 Javascript
通过实例讲解JS如何防抖动
2019/06/15 Javascript
Vue路由管理器Vue-router的使用方法详解
2020/02/05 Javascript
koa-passport实现本地验证的方法示例
2020/02/20 Javascript
Python中的Matplotlib模块入门教程
2015/04/15 Python
windows系统下Python环境的搭建(Aptana Studio)
2017/03/06 Python
更换Django默认的模板引擎为jinja2的实现方法
2018/05/28 Python
详解Python 协程的详细用法使用和例子
2018/06/15 Python
Python3.6使用tesseract-ocr的正确方法
2018/10/17 Python
python opencv将图片转为灰度图的方法示例
2019/07/31 Python
Python配置pip国内镜像源的实现
2020/08/20 Python
匡威比利时官网:Converse Belgium
2017/04/13 全球购物
理肤泉美国官网:La Roche-Posay
2018/01/17 全球购物
联想德国官网:Lenovo Germany
2018/07/04 全球购物
Coltorti Boutique官网:来自意大利的设计师品牌买手店
2018/11/09 全球购物
文明礼仪小标兵事迹
2014/01/12 职场文书
计算机科学技术自荐信
2014/06/12 职场文书
三严三实心得体会范文
2014/10/13 职场文书
初中政治教学工作总结
2015/08/13 职场文书
nginx结合openssl实现https的方法
2021/07/25 Servers
Java面试题冲刺第十八天--Spring框架3
2021/08/07 面试题