Python模块_PyLibTiff读取tif文件的实例


Posted in Python onJanuary 13, 2020

Usage example (libtiff wrapper)

from libtiff import TIFF
# to open a tiff file for reading:
tif = TIFF.open('filename.tif', mode='r')
# to read an image in the currect TIFF directory and return it as numpy array:
image = tif.read_image()
# to read all images in a TIFF file:
for image in tif.iter_images(): # do stuff with image
# to open a tiff file for writing:
tif = TIFF.open('filename.tif', mode='w')
# to write a image to tiff file
tif.write_image(image)

Usage example (pure Python module)

from libtiff import TIFFfile, TIFFimage
# to open a tiff file for reading
tif = TIFFfile('filename.tif')
# to return memmaps of images and sample names (eg channel names, SamplesPerPixel>=1)
samples, sample_names = tiff.get_samples()
# to create a tiff structure from image data
tiff = TIFFimage(data, description='')
# to write tiff structure to file
tiff.write_file('filename.tif', compression='none') # or 'lzw'
del tiff # flushes data to disk
from libtiff import TIFF 
from scipy import misc 
 
##tiff文件解析成图像序列 
##tiff_image_name: tiff文件名; 
##out_folder:保存图像序列的文件夹 
##out_type:保存图像的类型,如.jpg、.png、.bmp等 
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 
 
##图像序列保存成tiff文件 
##image_dir:图像序列所在文件夹 
##file_name:要保存的tiff文件名 
##image_type:图像序列的类型 
##image_num:要保存的图像数目 
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

用opencv读取

import cv2


cv2.imread("filename",flags)
对于cv2,imread的关于通道数和位深的flags有四种选择:

IMREAD_UNCHANGED = -1#不进行转化,比如保存为了16位的图片,读取出来仍然为16位。
IMREAD_GRAYSCALE = 0#进行转化为灰度图,比如保存为了16位的图片,读取出来为8位,类型为CV_8UC1。
IMREAD_COLOR = 1#进行转化为RGB三通道图像,图像深度转为8位
IMREAD_ANYDEPTH = 2#保持图像深度不变,进行转化为灰度图。
IMREAD_ANYCOLOR = 4#若图像通道数小于等于3,则保持原通道数不变;若通道数大于3则只取取前三个通道。图像深度转为8位
对于多通道TIFF图像,若要保证图像数据的正常读取,显然要选择IMREAD_UNCHANGED作为imread的flags设置值。

安装pylibtiff

##PIL使用

导入 Image 模块。然后通过 Image 类中的 open 方法即可载入一个图像文件。如果载入文件失败,则会引起一个 IOError ;若无返回错误,则 open 函数返回一个 Image 对象。现在,我们可以通过一些对象属性来检查文件内容,即:

>>> import Image
>>> im = Image.open("j.jpg")
>>> print im.format, im.size, im.mode
JPEG (440, 330) RGB

Image 类的实例有 5 个属性,分别是:

format: 以 string 返回图片档案的格式(JPG, PNG, BMP, None, etc.);如果不是从打开文件得到的实例,则返回 None。

mode: 以 string 返回图片的模式(RGB, CMYK, etc.);完整的列表参见 官方说明·图片模式列表

size: 以二元 tuple 返回图片档案的尺寸 (width, height)

palette: 仅当 mode 为 P 时有效,返回 ImagePalette 示例

info: 以字典形式返回示例的信息

函数概貌。

Reading and Writing Images : open( infilename ) , save( outfilename ) Cutting and Pasting and Merging Images :

crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标

系统的原点(0, 0)是左上角。

paste() :

merge() :

>>> box = (100, 100, 200, 200)
 >>> region = im.crop(box)
 >>> region.show()
 >>> region = region.transpose(Image.ROTATE_180)
 >>> region.show()
 >>> im.paste(region, box)
 >>> im.show()

旋转一幅图片:

def roll(image, delta):
  "Roll an image sideways"

  xsize, ysize = image.size

  delta = delta % xsize
  if delta == 0: return image

  part1 = image.crop((0, 0, delta, ysize))
  part2 = image.crop((delta, 0, xsize, ysize))
  image.paste(part2, (0, 0, xsize-delta, ysize))
  image.paste(part1, (xsize-delta, 0, xsize, ysize))

  return image

几何变换

>>>out = im.resize((128, 128))           #
 >>>out = im.rotate(45)               #逆时针旋转 45 度角。
 >>>out = im.transpose(Image.FLIP_LEFT_RIGHT)    #左右对换。
 >>>out = im.transpose(Image.FLIP_TOP_BOTTOM)    #上下对换。
 >>>out = im.transpose(Image.ROTATE_90)       #旋转 90 度角。
 >>>out = im.transpose(Image.ROTATE_180)      #旋转 180 度角。
>>>out = im.transpose(Image.ROTATE_270)      #旋转 270 度角。

Image 类的 thumbnail() 方法可以用来制作缩略图。它接受一个二元数组作为缩略图的尺寸,然后将示例缩小到指定尺寸。

import os, sys
from PIL import Image

for infile in sys.argv[1:]:
  outfile = os.path.splitext(infile)[0] + ".thumbnail"
  if infile != outfile:
    try:
      im  = Image.open(infile)
      x, y = im.size
      im.thumbnail((x//2, y//2))
      im.save(outfile, "JPEG")
    except IOError:
      print "cannot create thumbnail for", infile

这里我们用 im.size 获取原图档的尺寸,然后以 thumbnail() 制作缩略图,大小则是原先图档的四分之一。同样,如果图档无法打开,则在终端上打印无法执行的提示。

PIL.Image.fromarray(obj, mode=None)

Creates an image memory from an object exporting the array interface (using the buffer protocol).

If obj is not contiguous, then the tobytes method is called and frombuffer() is used.

Parameters: 
obj ? Object with array interface
mode ? Mode to use (will be determined from type if None) See: Modes.
Returns: 
An image object.

New in version 1.1.6.

PIL文档

以上这篇Python模块_PyLibTiff读取tif文件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
用python实现的可以拷贝或剪切一个文件列表中的所有文件
Apr 30 Python
Python中使用装饰器时需要注意的一些问题
May 11 Python
Windows系统下使用flup搭建Nginx和Python环境的方法
Dec 25 Python
Python实现包含min函数的栈
Apr 29 Python
python实现各进制转换的总结大全
Jun 18 Python
python enumerate函数的使用方法总结
Nov 15 Python
python对列进行平移变换的方法(shift)
Jan 10 Python
python单线程文件传输的实例(C/S)
Feb 13 Python
Python使用Opencv实现图像特征检测与匹配的方法
Oct 30 Python
Python作用域与名字空间原理详解
Mar 21 Python
基于Python实现粒子滤波效果
Dec 01 Python
Scrapy+Selenium自动获取cookie爬取网易云音乐个人喜爱歌单
Feb 01 Python
python多线程实现代码(模拟银行服务操作流程)
Jan 13 #Python
Python timeit模块的使用实践
Jan 13 #Python
Python 列表的清空方式
Jan 13 #Python
Python SSL证书验证问题解决方案
Jan 13 #Python
python清空命令行方式
Jan 13 #Python
Pytorch GPU显存充足却显示out of memory的解决方式
Jan 13 #Python
Python开发之基于模板匹配的信用卡数字识别功能
Jan 13 #Python
You might like
CodeIgniter框架提示Disallowed Key Characters的解决办法
2014/04/21 PHP
非常全面的php日期时间运算汇总
2015/11/04 PHP
PHP如何防止XSS攻击与XSS攻击原理的讲解
2019/03/22 PHP
PHP程序守护进程化实现方法详解
2020/07/16 PHP
jquery.validate使用攻略 第一部
2010/07/01 Javascript
JavaScript动态改变HTML页面元素例如添加或删除
2014/08/10 Javascript
js怎么覆盖原有方法实现重写
2014/09/04 Javascript
JavaScript数组迭代方法
2017/03/03 Javascript
小程序实现发表评论功能
2018/07/06 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
小程序二次贝塞尔曲线实现购物车商品曲线飞入效果
2019/01/07 Javascript
es6数组之扩展运算符操作实例分析
2020/04/25 Javascript
微信小程序返回上一级页面的实现代码
2020/06/19 Javascript
Vue自定义全局弹窗组件操作
2020/08/11 Javascript
js实现表格单列按字母排序
2020/08/12 Javascript
详解django.contirb.auth-认证
2018/07/16 Python
Python常用的json标准库
2019/02/19 Python
Python虚拟环境venv用法详解
2020/05/25 Python
python实现时间序列自相关图(acf)、偏自相关图(pacf)教程
2020/06/03 Python
python实现简单的五子棋游戏
2020/09/01 Python
Kenneth Cole官网:纽约时尚优雅品牌
2016/11/14 全球购物
英国复古皮包品牌:Beara Beara
2018/07/18 全球购物
StubHub澳大利亚:购买或出售您的门票
2019/08/01 全球购物
儿科护理实习自我鉴定
2013/09/19 职场文书
简历的自荐信
2013/12/19 职场文书
公司行政经理岗位职责
2013/12/24 职场文书
运动会解说词200字
2014/02/06 职场文书
党员教师工作决心书
2014/03/13 职场文书
应届本科毕业生求职信
2014/07/23 职场文书
单位工作证明范文
2014/09/14 职场文书
个人求职意向书
2015/05/11 职场文书
初中毕业生感言
2015/07/31 职场文书
小学语文教学随笔
2015/08/14 职场文书
2016年五一劳动节专题校园广播稿
2015/12/17 职场文书
英文诗歌翻译方法(赏析)
2019/08/16 职场文书
使用Pytorch训练two-head网络的操作
2021/05/28 Python