-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
153 lines (126 loc) · 4.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from DicomManager.unzip import Unzipper
from DicomManager.DICOM import DICOM2JPEG
import os
from reportlab.platypus import Image as rlImage
from reportlab.lib import colors
from PDFMAKER.pdfmaker import MkPDF
from PyPDF2 import PdfMerger
path = "your path"
def Merger(path:str):
print("Analisando PDFs...")
path = path
try:
prefixes = set()
pdf_pairs = []
# Identificar os prefixos dos arquivos
for file in os.listdir(path):
if " " in file:
prefix = file.split(" ")[0]
prefixes.add(prefix)
# Verificar se os arquivos de cada prefixo existem
for prefix in prefixes:
pdf1 = f"{prefix}.pdf"
pdf2 = None
for file in os.listdir(path):
if file.startswith(prefix) and " " in file:
pdf2 = file
break
if (
pdf2
and os.path.exists(os.path.join(path, pdf1))
and os.path.exists(os.path.join(path, pdf2))
):
pdf_pairs.append((pdf1, pdf2))
# Realizar a fusão dos arquivos PDF
for pdf1, pdf2 in pdf_pairs:
if os.path.exists(f"Laudo_{pdf2[:-4]}.pdf"):
print("Nada a unir!")
continue
else:
output_filename = f"Laudo_{pdf2[:-4]}.pdf"
merger = PdfMerger()
merger.append(os.path.join(path, pdf1))
merger.append(os.path.join(path, pdf2))
merger.write(output_filename)
merger.close()
print("PDFs Unidos!")
except Exception as e:
print(e)
def Extract_Convert_Img(file: str):
"""Function to extract and convert DICOM images to JPEG format.
#### Parameters:
- file: str
Name of the ZIP file containing the DICOM images."""
# Extract the file
unzipper = Unzipper(f"{file}", "./Dicoms")
unzipper.unzipper()
name = unzipper.name
print(name)
dicom2jpeg = DICOM2JPEG("./Dicoms", "./Images")
dicom2jpeg.converter()
MkPDF(name)
dicom2jpeg.eliminate_folders()
# Test for a any other pdf file on the current folder
print("Extração e conversão concluídas com sucesso!")
def ExtractSR(file: str):
"""Function to extract and convert DICOM images to JPEG format.
#### Parameters:
- file: str
Name of the ZIP file containing the DICOM images."""
# Extract the file
unzipper = Unzipper(f"{file}", "./Dicoms")
unzipper.unzipper()
name = unzipper.name
print(name)
dicom2jpeg = DICOM2JPEG("./Dicoms", "./Images")
dicom2jpeg.converter()
MkPDF(name)
dicom2jpeg.eliminate_folders()
# Test for a any other pdf file on the current folder
print("Extração e conversão concluídas com sucesso!")
# ORTHANC
from pyorthanc import Orthanc
import time
import json
orthanc = Orthanc("yourserver", "orthanc", "orthanc")
def connection():
max_attempts = 5 # Número máximo de tentativas antes de abortar
attempt = 0
while True:
try:
while True:
# Load pacientes atualizados
patients = None
with open("patients.json") as json_file:
patients = json.load(json_file)
# Atualização os pacientes
latest_patients = orthanc.get_patients()
if patients == latest_patients:
print("Nenhum novo paciente encontrado")
Merger(path)
time.sleep(15)
else:
new_patients = [p for p in latest_patients if p not in patients]
# Processo para cada novo paciente
for patient in new_patients:
response = orthanc.get_patients_id_archive(str(patient))
print(f"Baixando paciente {patient}...")
with open(f"ZIPS/{patient}.zip", "wb") as f:
f.write(response)
Extract_Convert_Img(f"{patient}.zip")
# Atualizar pacientes
patients = latest_patients
with open("patients.json", "w") as json_file:
json.dump(patients, json_file)
# Esperar um momento (tempo em segundos) antes de verificar novamente
time.sleep(15)
except Exception as e:
attempt += 1
print(f"Ocorreu um erro: {e}. Tentativa {attempt} de {max_attempts}")
if attempt >= max_attempts:
print("Número máximo de tentativas atingido. Abortando.")
break
else:
connection()
time.sleep(5) # Pequeno delay antes de tentar novamente
connection()