【python圖像處理】tiff檔案的儲存與解析
tiff檔案是一種常用的圖像檔案格式,支援将多幅圖像儲存到一個檔案中,極大得友善了圖像的儲存和處理。python中支援tiff檔案處理的是libtiff子產品中的TIFF類(libtiff下載下傳連結https://pypi.python.org/pypi/libtiff/)。
這裡主要介紹tiff檔案的解析和儲存,具體見如下代碼:
<b>[python]</b> view plain copy
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()