Python模組_PyLibTiff讀取tif檔案的例項
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,delta,ysize)) part2 = image.crop((delta,xsize,ysize)) image.paste(part2,(0,xsize-delta,ysize)) image.paste(part1,(xsize-delta,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檔案的例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。