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

 更新时间:2020年4月27日 21:20  点击:2196

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文件的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • python opencv 画外接矩形框的完整代码

    这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • 最炫Python烟花代码全解析

    2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
  • python中numpy.empty()函数实例讲解

    在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
  • python-for x in range的用法(注意要点、细节)

    这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • python实现b站直播自动发送弹幕功能

    这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
  • python Matplotlib基础--如何添加文本和标注

    这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • python 计算方位角实例(根据两点的坐标计算)

    今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
  • python实现双色球随机选号

    这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02
  • python中使用np.delete()的实例方法

    在本篇文章里小编给大家整理的是一篇关于python中使用np.delete()的实例方法,对此有兴趣的朋友们可以学习参考下。...2021-02-01
  • 使用Python的pencolor函数实现渐变色功能

    这篇文章主要介绍了使用Python的pencolor函数实现渐变色功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-09
  • python自动化办公操作PPT的实现

    这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
  • Python getsizeof()和getsize()区分详解

    这篇文章主要介绍了Python getsizeof()和getsize()区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-20
  • 解决python 两个时间戳相减出现结果错误的问题

    这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
  • python实现学生通讯录管理系统

    这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
  • PyTorch一小时掌握之迁移学习篇

    这篇文章主要介绍了PyTorch一小时掌握之迁移学习篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-08
  • Python绘制的爱心树与表白代码(完整代码)

    这篇文章主要介绍了Python绘制的爱心树与表白代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-06