Python模塊_PyLibTiff讀取tif文件的實(shí)例
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:要保存的圖像數(shù)目 def image_array_to_tiff(image_dir, file_name, image_type, image_num): out_tiff = TIFF.open(file_name, mode = 'w') #這里假定圖像名按序號(hào)排列 for i in range(0, image_num): image_name = image_dir + str(i) + image_type image_array = Image.open(image_name) #縮放成統(tǒng)一尺寸 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)
對(duì)于cv2,imread的關(guān)于通道數(shù)和位深的flags有四種選擇: IMREAD_UNCHANGED = -1#不進(jìn)行轉(zhuǎn)化,比如保存為了16位的圖片,讀取出來仍然為16位。 IMREAD_GRAYSCALE = 0#進(jìn)行轉(zhuǎn)化為灰度圖,比如保存為了16位的圖片,讀取出來為8位,類型為CV_8UC1。 IMREAD_COLOR = 1#進(jìn)行轉(zhuǎn)化為RGB三通道圖像,圖像深度轉(zhuǎn)為8位 IMREAD_ANYDEPTH = 2#保持圖像深度不變,進(jìn)行轉(zhuǎn)化為灰度圖。 IMREAD_ANYCOLOR = 4#若圖像通道數(shù)小于等于3,則保持原通道數(shù)不變;若通道數(shù)大于3則只取取前三個(gè)通道。圖像深度轉(zhuǎn)為8位 對(duì)于多通道TIFF圖像,若要保證圖像數(shù)據(jù)的正常讀取,顯然要選擇IMREAD_UNCHANGED作為imread的flags設(shè)置值。
##PIL使用
導(dǎo)入 Image 模塊。然后通過 Image 類中的 open 方法即可載入一個(gè)圖像文件。如果載入文件失敗,則會(huì)引起一個(gè) IOError ;若無返回錯(cuò)誤,則 open 函數(shù)返回一個(gè) Image 對(duì)象?,F(xiàn)在,我們可以通過一些對(duì)象屬性來檢查文件內(nèi)容,即:
>>> import Image >>> im = Image.open("j.jpg") >>> print im.format, im.size, im.mode JPEG (440, 330) RGB
Image 類的實(shí)例有 5 個(gè)屬性,分別是:
format: 以 string 返回圖片檔案的格式(JPG, PNG, BMP, None, etc.);如果不是從打開文件得到的實(shí)例,則返回 None。
mode: 以 string 返回圖片的模式(RGB, CMYK, etc.);完整的列表參見 官方說明·圖片模式列表
size: 以二元 tuple 返回圖片檔案的尺寸 (width, height)
palette: 僅當(dāng) mode 為 P 時(shí)有效,返回 ImagePalette 示例
info: 以字典形式返回示例的信息
函數(shù)概貌。
Reading and Writing Images : open( infilename ) , save( outfilename ) Cutting and Pasting and Merging Images :
crop() : 從圖像中提取出某個(gè)矩形大小的圖像。它接收一個(gè)四元素的元組作為參數(shù),各元素為(left, upper, right, lower),坐標(biāo)
系統(tǒng)的原點(diǎn)(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()
旋轉(zhuǎn)一幅圖片:
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) #逆時(shí)針旋轉(zhuǎn) 45 度角。 >>>out = im.transpose(Image.FLIP_LEFT_RIGHT) #左右對(duì)換。 >>>out = im.transpose(Image.FLIP_TOP_BOTTOM) #上下對(duì)換。 >>>out = im.transpose(Image.ROTATE_90) #旋轉(zhuǎn) 90 度角。 >>>out = im.transpose(Image.ROTATE_180) #旋轉(zhuǎn) 180 度角。 >>>out = im.transpose(Image.ROTATE_270) #旋轉(zhuǎn) 270 度角。
Image 類的 thumbnail() 方法可以用來制作縮略圖。它接受一個(gè)二元數(shù)組作為縮略圖的尺寸,然后將示例縮小到指定尺寸。
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() 制作縮略圖,大小則是原先圖檔的四分之一。同樣,如果圖檔無法打開,則在終端上打印無法執(zhí)行的提示。
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.
以上這篇Python模塊_PyLibTiff讀取tif文件的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)讀取mat、tif和hdr格式數(shù)據(jù)
- Python讀取TIF影像的多種方法
- python讀取與寫入tif圖片的完整信息(過程詳解)
- Python讀取hdf文件并轉(zhuǎn)化為tiff格式輸出
- 利用Python裁切tiff圖像且讀取tiff,shp文件的實(shí)例
- python讀取tif圖片時(shí)保留其16bit的編碼格式實(shí)例
- 對(duì)Python3+gdal 讀取tiff格式數(shù)據(jù)的實(shí)例講解
- 淺談python下tiff圖像的讀取和保存方法
- Python讀取TIF文件的兩種方法實(shí)現(xiàn)
相關(guān)文章
Python實(shí)現(xiàn)基于Fasttext的商品評(píng)論數(shù)據(jù)分類的操作流程
這篇文章主要介紹了Python實(shí)現(xiàn)基于Fasttext的商品評(píng)論數(shù)據(jù)分類,今天使用的fasttext更像是一個(gè)集成的庫(kù),把向量化和分類一起做掉了,這個(gè)對(duì)于使用層面來講就更方便了一些,需要的朋友可以參考下2022-06-06python開發(fā)之for循環(huán)操作實(shí)例詳解
這篇文章主要介紹了python開發(fā)之for循環(huán)操作,以實(shí)例形式較為詳細(xì)的分析了Python中for循環(huán)的具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11python實(shí)現(xiàn)監(jiān)控某個(gè)服務(wù) 服務(wù)崩潰即發(fā)送郵件報(bào)告
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)監(jiān)控某個(gè)服務(wù),服務(wù)崩潰發(fā)送郵件報(bào)告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06python 數(shù)據(jù)清洗之?dāng)?shù)據(jù)合并、轉(zhuǎn)換、過濾、排序
這篇文章主要介紹了python 數(shù)據(jù)清洗之?dāng)?shù)據(jù)合并、轉(zhuǎn)換、過濾、排序的相關(guān)資料,需要的朋友可以參考下2017-02-02Requests庫(kù)實(shí)現(xiàn)數(shù)據(jù)抓取與處理功能
本文介紹了Python中常用的第三方庫(kù)Requests的基本用法和高級(jí)功能,我們學(xué)習(xí)了如何發(fā)起HTTP請(qǐng)求、處理響應(yīng)、使用會(huì)話對(duì)象、設(shè)置代理和證書驗(yàn)證等技巧,需要的朋友可以參考下2023-05-05基于Python實(shí)現(xiàn)自動(dòng)用小寫字母替換文件后綴的大寫字母
本文介紹基于Python語言,基于一個(gè)大文件夾,遍歷其中的多個(gè)子文件夾,對(duì)于每一個(gè)子文件夾中的大量文件,批量將其文件的名稱或后綴名中的字母由大寫修改為小寫的方法,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04