使用pydicom 批量修改dcm文件tag

一个pydicom类,可以用来单个或者批量修改dicom文件。

#该类主要用于测试pydicom模块
#可以快速的对dicom进行数据处理

import pydicom
from matplotlib import pyplot as plt
import os

class DicomTool:
    m_path = None
    m_dicom = None

    def __init__(self, path):
        self.m_path = path
        self.m_dicom = pydicom.dcmread(path)

    def show_all_tag(self):
        print(self.m_dicom)

    def show_one_tag(self,tag):
        oneTag = self.m_dicom[tag]
        print(oneTag)

    def show_pixelDate(self):
        if 'PixelData' in self.m_dicom:
            rows = int(self.m_dicom.Rows)
            cols = int(self.m_dicom.Columns)
            print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
                rows=rows, cols=cols, size=len(self.m_dicom.PixelData)))
            if 'PixelSpacing' in self.m_dicom:
                print("Pixel spacing....:", self.m_dicom.PixelSpacing)

        plt.imshow(self.m_dicom.pixel_array, cmap=plt.cm.bone)
        plt.show()

    def change_tag(self, tag, value):
        self.m_dicom[tag].value = value

    def save_dcm(self, savePath):
        self.m_dicom.save_as(savePath)

    def get_tag(self, tag):
        return self.m_dicom.get_item(tag)


if __name__ == '__main__':

    # srcDir = r"G:\Break"
    #
    # if srcDir[-1] != '\\':
    #     srcDir = srcDir + '\\'
    #
    # desDir = "D:/desDir"
    # if not os.path.exists(desDir):
    #     os.makedirs(desDir)
    #
    # dcms = os.listdir(srcDir)
    # for dcm in dcms:
    #     dcmpath = srcDir + dcm
    #     print(dcmpath)
    #
    #     with open(dcmpath, "wb") as f:
    #         f.seek(0X97f0)
    #         f.write(b'\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33\x11\x22\x33')


    #批量修改
    srcDir = r"F:\1.2.156.112605.198558785608584.230924064402.6.30360.11515\1.2.156.112605.198558785608584.230924062726.3.14176.34408"

    if srcDir[-1] != '\\':
        srcDir = srcDir + '\\'

    desDir = "D:/desDir"
    if not os.path.exists(desDir):
        os.makedirs(desDir)

    dcms = os.listdir(srcDir)
    for dcm in dcms:
        dcmpath = srcDir + dcm
        print(dcmpath)

        myTool = DicomTool(dcmpath)
        SopUID = myTool.get_tag(0x00100010).value.decode(encoding="utf-8", errors="strict")
        print(SopUID)
        myTool.change_tag(0x00280008, '')
        myTool.save_dcm(desDir + '/' + dcm)



    # #单一修改
    # myTool = DicomTool(r"C:\Users\xinzhe.zhou\Desktop\1_Coronary_1.dcm")
    # # myTool = DicomTool("d.dcm")
    # # myTool.show_all_tag()
    # # myTool.show_pixelDate()
    # # myTool.show_one_tag(0x0008103E)
    # myTool.change_tag(0x00280030, "")
    # #
    # myTool.save_dcm(r"G:\1_Coronary_11.dcm")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值