Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合
生活随笔
收集整理的這篇文章主要介紹了
Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用opencv讀取tif 文件
#導入cv模塊 import cv2 as cv import numpy as np #讀取圖像,支持 bmp、jpg、png、tiff 等常用格式 #第二個參數是通道數和位深的參數,有四種選擇 img = cv.imread("filename.tif",2) print img #在這里一開始我寫成了img.shape(),報錯因為img是一個數組不是一個函數,只有函數才可以加()表示請求執行 print img.shape print img.dtype print img.min() print img.max() #創建窗口并顯示圖像 cv.namedWindow("Image") cv.imshow("Image",img) cv.waitKey(0)#釋放窗口 cv.destroyAllWindows()對于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則只取取前三個通道。圖像深度轉為8PIL讀取圖像
from PIL import Image im = Image.open("filename")支持單通道及多通道Uint8 TIFF圖像讀取,讀取單通道Uint16 TIFF圖像轉為Uint8處理,直接讀取Uint16 TIFF圖像會報錯。
LIBTIFF包讀取保存圖像
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 filetif.write_image(image)scikit包讀取保存圖像
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' from skimage import io img = io.imread('testimg.tif') import numpy as np data=np.random.random([100,100]) io.imsave('rand_data.tif',np.float32(data))imageio包讀取保存圖像
import imageio im = imageio.imread('imageio:chelsea.png') # read a standard image im.shape # im is a numpy array (300, 451, 3) imageio.imwrite('~/chelsea-gray.jpg', im[:, :, 0])misc包讀取保存圖像
from scipy import misc# 讀入已有圖像,數據類型和原圖像一致 tif32 = misc.imread('.\test\lena32.tif') #<class 'numpy.float32'=""> tif16 = misc.imread('.\test\lena16.tif') #<class 'numpy.uint16'=""> tif8 = misc.imread('.\test\lena8.tif') #<class 'numpy.uint8'="">#保存圖像 misc.imsave('.\test\lena32_scipy.tif', tif32) #--> 8bit(tif16和tif8同) misc.imsave('.\test\\randmat64_scipy.tif', flt) #--> 8bit misc.imsave('.\test\\randmat8_scipy.tif', z8) #--> 8bit(z16和z32同)總結
以上是生活随笔為你收集整理的Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3 操作符重载方法
- 下一篇: Python十段经典代码