def store(host, port, calling_ae_title, called_ae_title, filenames): transfer_syntaxes = [ odil.registry.ExplicitVRLittleEndian, odil.registry.ImplicitVRLittleEndian, ] # Find all SOP classes to negotiate at association time. We don't need to # read the whole data set for this sop_classes = set() for filename in filenames: _, data_set = odil.read( filename, halt_condition=lambda tag: tag>odil.registry.SOPClassUID) sop_classes.update(data_set.as_string("SOPClassUID")) print(sop_classes) presentation_contexts = [ odil.AssociationParameters.PresentationContext( 2*i+1, sop_class, transfer_syntaxes, True, False) for i, sop_class in enumerate(sop_classes) ] # Create the association and the Store SCU association = odil.Association() association.set_peer_host(host) association.set_peer_port(port) association.update_parameters()\ .set_calling_ae_title(calling_ae_title)\ .set_called_ae_title(called_ae_title)\ .set_presentation_contexts(presentation_contexts) association.associate() negotiated_parameters = association.get_negotiated_parameters() negotiated_pc = negotiated_parameters.get_presentation_contexts() for pc in negotiated_pc: print(pc.abstract_syntax, " ", pc.transfer_syntaxes[0]) store = odil.StoreSCU(association) for filename in filenames: _, data_set = odil.read(filename) try: store.set_affected_sop_class(data_set) store.store(data_set) except Exception as e: print("Could not store {}: {}".format(filename, e)) association.release()
def test_respondDICOM(self): wado = odil.webservices.WADORSResponse() wado.set_data_sets(self.data_sets) wado.respond_dicom(odil.webservices.Utils.Representation.DICOM) self.assertEqual(wado.get_type(), odil.webservices.Utils.Type.DICOM) self.assertEqual(wado.get_representation(), odil.webservices.Utils.Representation.DICOM) http = wado.get_http_response() # Convert http response into string http_str = "" h = [] for header in http.get_headers(): h.append(header.key()) for header in h: http_str += header + ": " + http.get_header(header) http_str += "\r\n" + http.get_body() msg = email.message_from_string(http_str) self.assertTrue(msg.is_multipart()) i = 0 for part in msg.walk(): if part.get_content_type() == "application/dicom": file = tempfile.mkstemp() stream = io.FileIO(file[1], 'w') stream.write(part.get_payload()) stream.close() ds = odil.read(file[1])[1] self.assertEqual(self.data_sets[i], ds) i = i + 1 self.assertEqual(i, len(self.data_sets))
def convert(input, output, transfer_syntax, item_length, use_group_length): header, data_set = odil.read(input) to_remove = [ "FileMetaInformationVersion", "MediaStorageSOPClassUID", "MediaStorageSOPInstanceUID", "TransferSyntaxUID", "ImplementationClassUID", "ImplementationVersionName" ] for name in to_remove: header.remove(getattr(odil.registry, name)) odil.write(data_set, output, header, transfer_syntax, item_length, use_group_length)
def print_(inputs, print_header, decode_uids): for input in inputs: logging.info("Printing {}".format(input)) header, data_set = odil.read(input) max_length = find_max_name_length(data_set) if print_header: max_length = max(max_length, find_max_name_length(header)) if print_header: print_data_set(header, decode_uids, "", max_length) print() print_data_set(data_set, decode_uids, "", max_length)
def main(): parser = argparse.ArgumentParser(description="Print content of DICOM file") parser.add_argument("file") parser.add_argument( "--header", "-H", action="store_true", help="Print header") parser.add_argument( "--decode-uids", "-D", action="store_true", help="Display UID names") arguments = parser.parse_args() header, data_set = odil.read(arguments.file) if arguments.header: print_data_set(header, arguments.decode_uids) print print_data_set(data_set, arguments.decode_uids)
def run_client(self): command = [ "findscu", "-P", "-k", "QueryRetrieveLevel=PATIENT", "-k", "PatientID=*", "-k", "PatientName", "-q", "-X", "localhost", "11113" ] retcode = subprocess.call(command) if retcode != 0: return [] files = sorted(glob.glob("rsp*")) data_sets = [odil.read(x)[1] for x in files] for file_ in files: os.remove(file_) return data_sets
def run_client(self): command = [ "getscu", "-ll", "error", "-P", "-k", "QueryRetrieveLevel=PATIENT", "-k", "PatientID=*", "-k", "PatientName", "+B", "localhost", "11113" ] retcode = subprocess.call(command) if retcode != 0: return [] files = sorted(glob.glob("{}*".format(odil.uid_prefix))) data_sets = [odil.read(x)[1] for x in files] for file_ in files: os.remove(file_) return data_sets
def main(): parser = argparse.ArgumentParser(description="Print content of DICOM file") parser.add_argument("file") parser.add_argument("--header", "-H", action="store_true", help="Print header") parser.add_argument("--decode-uids", "-D", action="store_true", help="Display UID names") arguments = parser.parse_args() header, data_set = odil.read(arguments.file) if arguments.header: print_data_set(header, arguments.decode_uids) print print_data_set(data_set, arguments.decode_uids)
def run_client(self): command = [ "getscu", "-ll", "error", "-P", "-k", "QueryRetrieveLevel=PATIENT", "-k", "PatientID=*", "-k", "PatientName", "+B", "localhost", "11113"] retcode = subprocess.call(command) if retcode != 0: return [] files = sorted(glob.glob("{}*".format(odil.uid_prefix))) data_sets = [odil.read(x)[1] for x in files] for file_ in files: os.remove(file_) return data_sets
def as_json(input, output, pretty_print): _, data_set = odil.read(input) with open(output, "w") as fd: json = odil.as_json(data_set, pretty_print) fd.write(json)
def as_binary(input, output, transfer_syntax): _, data_set = odil.read(input) odil.write(data_set, output, transfer_syntax=transfer_syntax)
def as_xml(input, output, pretty_print): _, data_set = odil.read(input) with open(output, "w") as fd: xml = odil.as_xml(data_set, pretty_print) fd.write(xml)