Many medical images are raw Format saved ,raw At the same time, it is also the original format of the photos taken by the SLR camera .
RAW Images :CMOS perhaps CCD The image sensor converts the captured light source signal into the original data of the digital signal .
Recently used pytorch When processing ophthalmic medical images , Need to put raw Convert to jpg. because raw Generally, images cannot be processed directly , So use Python Format conversion . After searching, it's very complicated , What to get raw Data format , The length and width of the picture , As a result, I tried it myself , direct opencv The result is , So record it , There is no in-depth study , I look forward to further research in the future raw Image processing .
Core code
img = cv2.imread(filePath)
cv2.imwrite(filename, img)
Complete code :jpg.py
import numpy as np
import os
import cv2
import shutil
from PIL import Image
def searchDirFile(rootDir,saveDir):
for dir_or_file in os.listdir(rootDir):
try:
filePath = os.path.join(rootDir, dir_or_file)
# Determine if it is a file
if os.path.isfile(filePath):
# If it is a document, then judge whether it is based on .jpg ending , If not, skip this cycle
if os.path.basename(filePath).endswith('.raw'):
print('imgBox fileName is: '+os.path.basename(filePath))
# Copy jpg File to the directory you want to save
# shutil.copyfile(filePath,os.path.join(saveDir,os.path.basename(filePath)))
img = cv2.imread(filePath)
path2 = filePath.split('/')[2]
path = f'{saveDir}/{path2}'
# print(path)
if not os.path.exists(path):
os.makedirs(path)
filename = os.path.splitext(os.path.basename(filePath))[0] + ".jpg"
filename = os.path.join(path, filename)
print('filename is: '+filename)
cv2.imwrite(filename, img)
else:
continue
# If it's a dir, Then call this function again , Incoming current directory , Recursive processing .
elif os.path.isdir(filePath):
searchDirFile(filePath, saveDir)
else: print('not file and dir '+os.path.basename(filePath))
except:
continue
if __name__ == '__main__':
rootDir = './Images'
saveDir = './jpg'
searchDirFile(rootDir, saveDir)
print("the end !!!")