Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合
生活随笔
收集整理的這篇文章主要介紹了
Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
利用opencv讀取tif 文件
#導(dǎo)入cv模塊 import cv2 as cv import numpy as np #讀取圖像,支持 bmp、jpg、png、tiff 等常用格式 #第二個(gè)參數(shù)是通道數(shù)和位深的參數(shù),有四種選擇 img = cv.imread("filename.tif",2) print img #在這里一開(kāi)始我寫(xiě)成了img.shape(),報(bào)錯(cuò)因?yàn)閕mg是一個(gè)數(shù)組不是一個(gè)函數(shù),只有函數(shù)才可以加()表示請(qǐng)求執(zhí)行 print img.shape print img.dtype print img.min() print img.max() #創(chuàng)建窗口并顯示圖像 cv.namedWindow("Image") cv.imshow("Image",img) cv.waitKey(0)#釋放窗口 cv.destroyAllWindows()對(duì)于cv2,imread的關(guān)于通道數(shù)和位深的flags有四種選擇:
IMREAD_UNCHANGED = -1#不進(jìn)行轉(zhuǎn)化,比如保存為了16位的圖片,讀取出來(lái)仍然為16位。 IMREAD_GRAYSCALE = 0#進(jìn)行轉(zhuǎn)化為灰度圖,比如保存為了16位的圖片,讀取出來(lái)為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)為8PIL讀取圖像
from PIL import Image im = Image.open("filename")支持單通道及多通道Uint8 TIFF圖像讀取,讀取單通道Uint16 TIFF圖像轉(zhuǎn)為Uint8處理,直接讀取Uint16 TIFF圖像會(huì)報(bào)錯(cuò)。
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包讀取保存圖像
''' 遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)! ''' 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# 讀入已有圖像,數(shù)據(jù)類型和原圖像一致 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同)總結(jié)
以上是生活随笔為你收集整理的Python 各种读取保存tif,tiff,png,jpg,mat等格式图像方法大集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python3 操作符重载方法
- 下一篇: Python十段经典代码