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入门教程之if语句的用法
May 14 Python
Tensorflow实现AlexNet卷积神经网络及运算时间评测
May 24 Python
django 发送邮件和缓存的实现代码
Jul 18 Python
Python-Tkinter Text输入内容在界面显示的实例
Jul 12 Python
Python 最强编辑器详细使用指南(PyCharm )
Sep 16 Python
python实现简单坦克大战
Mar 27 Python
Numpy 理解ndarray对象的示例代码
Apr 03 Python
Python列表去重复项的N种方法(实例代码)
May 12 Python
Python Pandas数据分析工具用法实例
Nov 05 Python
python基于OpenCV模板匹配识别图片中的数字
Mar 31 Python
手残删除python之后的补救方法
Jun 26 Python
Python内置数据类型中的集合详解
Mar 18 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
一个php作的文本留言本的例子(六)
2006/10/09 PHP
PHP4实际应用经验篇(2)
2006/10/09 PHP
通过PHP的内置函数,通过DES算法对数据加密和解密
2012/06/21 PHP
WordPress中制作导航菜单的PHP核心方法讲解
2015/12/11 PHP
php写app用的框架整理
2019/09/29 PHP
js单独获取一个checkbox看其是否被选中
2014/09/22 Javascript
javascript实现切换td中的值
2014/12/05 Javascript
jquery 构造函数在表单提交过程中修改数据
2015/05/25 Javascript
JS控制层作圆周运动的方法
2016/06/20 Javascript
JS实现获取剪贴板内容的方法
2016/06/21 Javascript
canvas滤镜效果实现代码
2017/02/06 Javascript
AngularJS常见过滤器用法实例总结
2017/07/06 Javascript
javascript事件循环event loop的简单模型解释与应用分析
2020/03/14 Javascript
python统计字符串中指定字符出现次数的方法
2015/04/04 Python
python简单实现旋转图片的方法
2015/05/30 Python
Python实现ssh批量登录并执行命令
2016/10/25 Python
简单实现Python爬取网络图片
2018/04/01 Python
python实现简单多人聊天室
2018/12/11 Python
pytorch获取vgg16-feature层输出的例子
2019/08/20 Python
简单了解python数组的基本操作
2019/11/26 Python
flask利用flask-wtf验证上传的文件的方法
2020/01/17 Python
python实现录屏功能(亲测好用)
2020/03/02 Python
Python 解决火狐浏览器不弹出下载框直接下载的问题
2020/03/09 Python
Python实现的北京积分落户数据分析示例
2020/03/27 Python
python 中的9个实用技巧,助你提高开发效率
2020/08/30 Python
Hotels.com印度:酒店预订
2019/05/11 全球购物
手工制作的男士奢华英国鞋和服装之家:Goodwin Smith
2019/06/21 全球购物
系统管理员的职责包括那些?管理的对象是什么?
2016/09/20 面试题
生产班组长岗位职责
2014/01/05 职场文书
医药个人求职信范文
2014/01/29 职场文书
“四风”问题自我剖析材料思想汇报
2014/09/23 职场文书
加强机关作风建设心得体会
2014/10/22 职场文书
2015年班组长工作总结
2015/04/10 职场文书
交通安全温馨提示语
2015/07/14 职场文书
超市主管竞聘书
2015/09/15 职场文书
用python基于appium模块开发一个自动收取能量的小助手
2021/09/25 Python