python opencv实现 tiff转raw格式以及扩充体积补零

##tiff转raw格式
import numpy as np
import imageio
import cv2
src = np.fromfile("路径名.tiff" , dtype = 'uint8')
src.astype(np.uint8)
src.tofile('D:/data/region/cut.raw')

##批量tiff转raw格式
import numpy as np
import imageio
import cv2
import os
path='路径名'
path2='路径名'
files=os.listdir(path)
row=512
col=512*2
channels=1
for file in files:
    portion=os.path.splitext(file)
    if portion[1]=='.tif':
        realpath=path+file
        src = np.fromfile(realpath , dtype = 'uint16')
        src.astype(np.uint16)
        fileName=path2+portion[0]+'.raw'
        src.tofile(fileName)

##tiff转raw格式,把512*512*n(n<512)补成512*512*512【可改成任何尺寸,这里以512为例】
import numpy as np
import imageio
import cv2
src = np.fromfile("路径名.tiff" , dtype = 'uint8')
print(src.shape)
b=[0]*512*512*512
src2=src
for i in range(src.shape[0]):
    b[i]=src[i]
c=np.array(b,dtype='uint8')
c.tofile('路径名.raw')

你可能感兴趣的:(numpy,python,opencv)