Python模块_PyLibTiff读取tif文件的实例
作者:ninesun11 发布时间:2023-04-11 03:22:06
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文档
来源:https://blog.csdn.net/u013381011/article/details/78414203
猜你喜欢
- 摘要本文主要介绍了利用python的 threading和queue库实现多线程编程,并封装为一个类,方便读者嵌入自己的业务逻辑。最后以机器
- 前言有时候在使用Python处理比较耗时操作的时候,为了便于观察处理进度,这时候就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了
- 如果备份的数据库有2个文件,分别是.LDF 和 .MDF,打开企业管理器,在实例上右击---所有任务--附加数据库,然后选择那个.MDF文件
- 相信很多人像我一样在学习python,pandas过程中对数据的选取和修改有很大的困惑(也许是深受Matlab)的影响。。。到今天终于完全搞
- 新闻系统、blog系统等都可能用到将动态页面生成静态页面的技巧来提高页面的访问速度,从而减轻服务器的压力,本文为大家搜集整理了ASP编程中常
- 一、is_numberic函数简介国内一部分CMS程序里面有用到过is_numberic函数,我们先看看这个函数的结构bool is_num
- 1 简介本篇主要介绍使用pytorch实现基于CharRNN来进行文本分类与内容生成所需要的相关知识,并最终给出完整的实现代码。2 相关AP
- 仿豆瓣分页原型(Javascript版)写了个分页的样式。自我感觉,这样的分页前后兼顾,对于用户的体验是蛮好使的Javascript分页代码
- JS获取多选框checkbox被选中的个数。var checkbox = document.getElementsByName("
- 300来行python代码实现简易版学生成绩管理系统,供大家参考,具体内容如下使用链表来实现class Node(object): def
- 初级的图像拼接为将两幅图像简单的粘贴在一起,仅仅是图像几何空间的转移与合成,与图像内容无关。高级图像拼接也叫作基于特征匹配的图像拼接,拼接时
- 根据Django官方文档介绍:A one-to-one relationship. Conceptually, this is simila
- 1.由于不支持博客首页到文章详情页的跳转,只能打开第一篇文章的详情页2.所以需要做以下工作:设计文章详情页的url,完善视图函数逻辑,实现首
- 1. 横排往下会影响阅读速度。如12345678的单排单列数字,肯定是竖排阅读快。但多行多列的整块信息,横排并不见得就比竖排慢,比如所有简体
- 这篇文章主要介绍了如何使用Python抓取网页tag操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 解决中文输入的两种应用:在脚本中加语言编码声明 “-*- coding: uft-8 -*-”应用一:print中出现中文方法一:用unic
- Tebsorflow开源实现多GPU训练cifar10数据集:cifar10_multi_gpu_train.pyTensorflow开源实
- 本文给出了MySQL数据库中定义外键的必要性、具体的定义步骤和相关的一些基本操作,供大家参考!定义数据表假如某个电脑生产商,它的数据库中保存
- 从事API相关的工作很有挑战性,在高峰期保持系统的稳定及健壮性就是其中之一,这也是我们在Mailgun做很多压力测试的原因。这么久以来,我们
- 这里列出了javascript 中的document.execCommand() 的各种参数说明:2D-Position 允许通过