-5

I have few files in my GCP bucket folder like below:

  1. image1.dicom
  2. image2.dicom
  3. image3
  4. file1
  5. file4.dicom

Now, I want to even check the files which has no extension i.e image3, file1 are dicom or not.

I use pydicom reader to read dicom files to get the data.

dicom_dataset = pydicom.dcmread("dicom_image_file_path")

Please suggest is there a way to validate the above two files are dicom or not in one sentence.

Shanti
  • 13
  • 4
  • I was using the above pydicom.dcmread to check for authentic dicom files. If this dcmread is able to read data from file then yes it's dicom file , else no. So to avoid this loop was looking for any other good approach. – Shanti Jan 10 '23 at 06:43

2 Answers2

2

You can use the pydicom.misc.is_dicom function or do:

try:
   ds = dcmread(filename)
except InvalidDicomError:
   pass
darcymason
  • 1,223
  • 6
  • 9
0

Darcy has the best general answer, if you're looking to check file types prior to processing them. Apart from checking is the file a dicom file, it will also make sure the file doesn't have any dicom problems itself.

However, another way to quickly check that may or may not be better, depending on your use case, is to simply check the file's signature (or magic number as they're sometimes known.

See https://en.wikipedia.org/wiki/List_of_file_signatures

Basically if the bytes from position 128 to 132 in the file are "DICM" then it should be a dicom file.

If you only want to check for 'is it dicom?' for a set of files, and do it very quickly, this might be another approach

Richard
  • 3,024
  • 2
  • 17
  • 40
  • Actually, the `is_dicom` function does exactly this. If it it True, you know you have a DICOM file. If it is False, it could still be one of the many DICOM files that does not have a preamble and "DICM" prefix. – darcymason Feb 05 '23 at 18:09
  • Cool ...learnt a new pydicom method :D – Richard Feb 06 '23 at 16:53