Это старая версия документа!
Есть у нас рентген аппарат у которого в xml файле написана кодировка ISO-8859-5, а русские символы в Windows-1251. Изменим тег с кодировкой и конвертируем кириллицу в латиницу с помощью python и отправим это все дело в ORTHANC.
ОСТОРОЖНО ! ГОВНОКОД !
Создадим базу данных:
-- phpMyAdmin SQL Dump -- version 5.0.4deb2 -- https://www.phpmyadmin.net/ -- -- Хост: localhost:3306 -- Время создания: Июн 16 2022 г., 10:49 -- Версия сервера: 10.5.15-MariaDB-0+deb11u1 -- Версия PHP: 7.4.28 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- База данных: `DicomConvert` -- -- -------------------------------------------------------- -- -- Структура таблицы `DicomSend` -- CREATE TABLE `DicomSend` ( `id` int(11) NOT NULL, `FileName` text NOT NULL, `SendDate` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), `PatientName` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Индексы сохранённых таблиц -- -- -- Индексы таблицы `DicomSend` -- ALTER TABLE `DicomSend` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT для сохранённых таблиц -- -- -- AUTO_INCREMENT для таблицы `DicomSend` -- ALTER TABLE `DicomSend` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Подключимся к базе:
cat connector.py import mysql.connector cnx = mysql.connector.connect(user='DicomConvert', password='d85El4459kqOq2q0', host='127.0.0.1', database='DicomConvert', use_pure=False)
server = '192.168.1.43 4242' - куда будем слать снимки
fileList = open(«/root/python/files_to_copy», «r») - путь до списка файлов, пример: /home/ololoev/DCM/777_888.DCM
Сам скрипт отправки:
cat dicomSend.py import glob import subprocess import os import signal import sys import time import fileinput import connector import transliterate server = '192.168.1.43 4242' fileList = open("/root/python/files_to_copy", "r") from datetime import datetime now= datetime.now() while True: lineFileList = fileList.readline() if not lineFileList: break filePath=lineFileList.strip() fileName=filePath.split('/')[-1] cursor = connector.cnx.cursor() select_query = (f"select * from DicomSend where FileName=%s") cursor.execute(select_query,[fileName]) row = cursor.fetchone() if row == None: dcmToXml=subprocess.Popen("dcm2xml +M +Wb "+ filePath + " " + filePath + ".xml", shell=True) dcmToXml.communicate() fin = open(filePath + ".xml",encoding = "WINDOWS-1251") fout = open(filePath +".xml.mod", "wt") for line in fin: fout.write( line.replace('ISO-8859-5', 'UTF-8') ) fin.close() fout.close() fullpathxml = filePath +'.xml.mod' import xml.etree.ElementTree as ET tree = ET.parse(fullpathxml) PatientName=tree.findall('data-set/element[@name="PatientName"]')[0].text if PatientName: PatientNameTrans=transliterate.transliterate(PatientName) else: PatientNameTrans="NameErr" tree.findall('data-set/element[@name="PatientName"]')[0].text = PatientNameTrans BodyPE=tree.findall('data-set/element[@name="BodyPartExamined"]')[0].text if BodyPE: BodyPETrans=transliterate.transliterate(BodyPE) else: BodyPETrans="BodyErr" tree.findall('data-set/element[@name="BodyPartExamined"]')[0].text = BodyPETrans try: ViewPos=tree.findall('data-set/element[@name="ViewPosition"]')[0].text ViewPosTrans=transliterate.transliterate(ViewPos) except: ViewPosTrans="ViewErr" tree.findall('data-set/element[@name="ViewPosition"]')[0].text = ViewPosTrans tree.write(fullpathxml) xmlToDcm=subprocess.Popen("xml2dcm -f "+ fullpathxml + " "+ fullpathxml +".dcm", shell=True) xmlToDcm.communicate() dcmSend=subprocess.Popen("dcmsend -nuc +ma "+server+" "+fullpathxml+".dcm", shell=True, text=True, stderr=subprocess.PIPE) data=dcmSend.communicate() for line in data: if line: line = line.strip() line=line.split(':')[0] if line=='W': cursor = connector.cnx.cursor() insert_query=(f"INSERT INTO DicomSend (id, FileName, SendDate, PatientName) values(NULL, %s, NULL, %s)") cursor.execute(insert_query,[fileName, PatientNameTrans]) connector.cnx.commit() fullpath='/'.join(fullpathxml.split('/')[:-1]) for xmlpath in glob.iglob(os.path.join(fullpath, '*.xml')): os.remove(xmlpath) for modpath in glob.iglob(os.path.join(fullpath, '*.mod')): os.remove(modpath) for moddcmpath in glob.iglob(os.path.join(fullpath, '*.mod.dcm')): os.remove(moddcmpath) else: print('ERROR. ORTHANC NOT ANSWER = ',now) else: print('RECORD IS DUPLICATED = ', now)
#!/bin/bash #!/bin/sh if ps -ef | grep -v grep | grep dicomSend.py ; then rsync -azvp --exclude 2020/ --exclude test.txt --exclude *.tgz /mnt/data/raid1/DicomShare/Roentgen /mnt/data/raid1/sendDCM/ ; rsync -azvp --exclude 2020/ --exclude test.txt --exclude *.tgz /mnt/data/raid1/DicomShare/MRT /mnt/data/raid1/sendDCM/ ; exit 0 else rsync -azvp --exclude 2020/ --exclude test.txt --exclude *.tgz /mnt/data/raid1/DicomShare/Roentgen /mnt/data/raid1/sendDCM/ ; rsync -azvp --exclude 2020/ --exclude test.txt --exclude *.tgz /mnt/data/raid1/DicomShare/MRT /mnt/data/raid1/sendDCM/ ; find /mnt/data/raid1/sendDCM/ -type f -mmin -2880 -a -iname "*.dcm" > /root/python/files_to_copy ; python /root/python/dicomSend.py >> /root/python/log/dicomConvert.log ; exit 0 fi