1717import mrcfile
1818import numpy as np
1919import png
20- import pydicom
2120import tifffile
2221from typing_extensions import Literal
2322
2726except ImportError :
2827 PIL = None # type: ignore[assignment, unused-ignore]
2928
29+ try :
30+ import pydicom
31+ except ImportError :
32+ pydicom = None # type: ignore[assignment, unused-ignore]
33+
3034from galaxy .datatypes .binary import Binary
3135from galaxy .datatypes .metadata import (
3236 FileParameter ,
@@ -541,11 +545,16 @@ def sniff(self, filename: str) -> bool:
541545 """
542546 Determine if the file is in DICOM format.
543547 """
544- try :
545- pydicom .dcmread (filename , stop_before_pixels = True )
546- return True
547- except pydicom .errors .InvalidDicomError :
548- return False
548+ if pydicom :
549+ try :
550+ pydicom .dcmread (filename , stop_before_pixels = True )
551+ return True
552+ except pydicom .errors .InvalidDicomError :
553+ return False
554+ else :
555+ with open (filename , "rb" ) as fh :
556+ fh .seek (128 )
557+ return fh .read (4 ) == b"DICM"
549558
550559 def get_mime (self ) -> str :
551560 """
@@ -555,6 +564,15 @@ def get_mime(self) -> str:
555564
556565 def set_meta (
557566 self , dataset : DatasetProtocol , overwrite : bool = True , metadata_tmp_files_dir : Optional [str ] = None , ** kwd
567+ ) -> None :
568+ """
569+ Populate the metadata of the DICOM file using the pydicom library, if available.
570+ """
571+ if pydicom :
572+ self ._set_meta_with_pydicom (dataset )
573+
574+ def _set_meta_with_pydicom (
575+ self , dataset : DatasetProtocol , overwrite : bool = True , metadata_tmp_files_dir : Optional [str ] = None , ** kwd
558576 ) -> None :
559577 """
560578 Populate the metadata of the DICOM file using the pydicom library.
0 commit comments