ディレクトリ作成して dicomファイルをコピーする python

新たに dicom ファイル構造を作成して、dicom ファイルを rename してをコピーします。

新しいファイル構造

以下のように、「pDICOM/西暦年/月/日/カルテ番号/検査ID」というディレクトリを作成して、その中に「検査ID_00000.dcm」から始まる通し番号を付けて dicom ファイルをコピーします。

「西暦年/月/日」は dcm4chee のものをそのまま持ってきます。
つまり、この数字は dicom ファイルをインポートした年月日です。


pDICOM
└── 2021
    └── 11
        └── 12
            ├── 20100761
            │   └── 9473
            │       ├── 9473_00000.dcm
            │       ├── 9473_00001.dcm
            │       ├── 9473_00002.dcm
            │       ├── 9473_00003.dcm
            │       ├── 9473_00004.dcm
            │       ├── 9473_00005.dcm
            │       ├── 9473_00006.dcm
            │       ├── 9473_00007.dcm
            │       ├── 9473_00008.dcm
            │       └── 9473_00009.dcm
            ├── 21202817
            │   └── 999
            │       ├── 999_00000.dcm
            │       ├── 999_00001.dcm
            │       ├── 999_00002.dcm
            │       ├── 999_00003.dcm
            │       ├── 999_00004.dcm
            │       ├── 999_00005.dcm
            │       └── 999_00006.dcm
            └── 9501731
                └── 976
                    ├── 976_00000.dcm
                    ├── 976_00001.dcm
                    ├── 976_00002.dcm
                    ├── 976_00003.dcm
                    ├── 976_00004.dcm
                    ├── 976_00005.dcm
                    ├── 976_00006.dcm
                    ├── 976_00007.dcm
                    ├── 976_00008.dcm
                    ├── 976_00009.dcm
                    └── 976_00010.dcm

python


import pydicom
import os
import re
import pymysql
import glob
import shutil

class Dicom:

    def getDicomArr( self, sd ):
        DicomArr = []
        for root, dir, files in os.walk( sd ):
            for file_ in files:
                full_path = os.path.join(root, file_)
                DicomArr.append( full_path )
        return DicomArr
    
    @classmethod 
    def getBdir(self, ef ):
        pattern = '\d{4}/\d{2}/\d{2}'
        res = re.search(pattern, ef)
        return res.group()

    def getDicomInfo( self, dcmArr ):
        dcmInfoArr = []
        studyIDArr =[]
        for eachFile in dcmArr:           
            ds = pydicom.read_file(eachFile)
            studyID = ds[0x0020, 0x0010].value
            if studyID not in studyIDArr:        
                studyDate = ds[0x0008, 0x0020].value
                studyTime = ds[0x0008, 0x0030].value
                modality = ds[0x0008, 0x0060].value
                try:
                    studyDscr = ds[0x0008, 0x1030].value
                except:
                    studyDscr = ''
                ptName = str(ds[0x0010, 0x0010].value).replace('^', ' ')
                karteNo = ds[0x0010, 0x0020].value
                sex = ds[0x0010, 0x0040].value
                birthday = ds[0x0010, 0x0030].value
                age = ds[0x0010, 0x1010].value
                institution = ds[0x0008, 0x0080].value
                bdir = Dicom.getBdir(eachFile)
                path = bdir + '/' + karteNo + '/' + studyID 
                thisLineInfo = [ studyID, studyDate, studyTime, modality, studyDscr, ptName, karteNo, birthday, sex, age, institution, path ]
                dcmInfoArr.append( thisLineInfo )
                studyIDArr.append( studyID )
        return dcmInfoArr
    
    def insertDB( self, DicomInfoArr ):           
        conn = pymysql.connect(host='localhost',
            user='root',
            db='pydcmdb',
            password='password',
            charset='utf8mb4',
            cursorclass=pymysql.cursors.DictCursor)
        try:
            with conn.cursor() as cursor:
                sql = "REPLACE INTO taginfo VALUES ( %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s )"
                cursor.executemany( sql, DicomInfoArr )
            conn.commit()
        finally:
            conn.close()

    def countDicom( self, dcmPath ):           
        searchDicom = dcmPath + '/*.dcm'
        files = []  
        files = glob.glob(searchDicom)   
        count = len(files)                   
        return count
        
    def copyDicomFile( self, dicomArr ):
        dicomFileDir = '/var/www/html/pDICOM/'
        for eachFile in dicomArr:
            ds = pydicom.read_file(eachFile)
            studyID = ds[0x0020, 0x0010].value
            karteNo = ds[0x0010, 0x0020].value
            firstDir = Dicom.getBdir( eachFile )
            destinationDir = dicomFileDir + firstDir + '/' + karteNo + '/' + studyID
            os.makedirs(destinationDir, exist_ok=True)
            dicomCount = self.countDicom(destinationDir)
            newFileName = studyID + "_%05d.dcm" %(dicomCount)
            destination = destinationDir + '/' + newFileName
            shutil.copyfile(eachFile, destination)

if __name__ == "__main__":
    dcm = Dicom()
    dcmArr = dcm.getDicomArr( '/var/www/html/DICOM/2021/11/12' )
    infos = dcm.getDicomInfo( dcmArr )
    dcm.insertDB( infos )
    dcm.copyDicomFile( dcmArr )