各種読み出し保存tif,tiff,png,jpg,matなどのフォーマット画像方法の大集合(python)

3136 ワード

Opencvでtifファイルを読み込む
#  cv  
import cv2 as cv
import numpy as np
#    ,   bmp、jpg、png、tiff      
#               ,     , https://www.cnblogs.com/goushibao/p/6671079.html
img = cv.imread("filename.tif",2)
print img
#          img.shape(),    img           ,        ()      ,
#  http://blog.csdn.net/a19990412/article/details/78283742
print img.shape
print img.dtype 
print img.min()
print img.max()
#         
cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)#    
cv.destroyAllWindows() 

cv 2の場合、imreadのチャネル数とビット深さに関するflagsには4つの選択肢があります.
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 

PIL読取画像
from PIL import Image
im = Image.open("filename")

シングルチャネルおよびマルチチャネルUIT 8 TIFF画像の読み取りをサポートし、シングルチャネルUIT 16 TIFF画像を読み取りUIT 8処理に移行し、直接UIT 16 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 file

tif.write_image(image)

scikitパッケージ読み出し保存画像

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') #
tif16 = misc.imread('.\test\lena16.tif') #
tif8 = misc.imread('.\test\lena8.tif') #

#    
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 )

 
 
matファイルもよく見かけるので、matファイルの読み書き保存も入れましょう!
scipy読み出し保存matファイル

import scipy.io as sio
# load mean and std
matfile = sio.loadmat(meanstd_file)
test_mean = np.array(matfile['mean_test'])
test_std = np.array(matfile['std_test'])

# Save predictions to a matfile to open later in matlab
mdict = {"Recovery": Recovery}
sio.savemat(savename, mdict
io.imsave('Recovery.tif', np.float32(Recovery))