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 cv2cv2.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.modeJPEG (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, sysfrom PIL import Imagefor 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 interfacemode – Mode to use (will be determined from type if None) See: Modes.Returns: An image object.New in version 1.1.6.
PIL文檔