Reading DICOM files uploaded through streamlit

Hello! Currently running into some issues when I try to pass the file object that’s returned from streamlit’s file_uploader into the pydicom.dcmread() function.

There are no issues with the file itself, since dcmread() works when given the path to the file. Below is an example of the error I get. Using the force=True as suggested does not resolve the issue.

Is there a workaround to get the file path from the file_uploader tool instead of a file object?

Thanks!

First question quickly, what version of Streamlit are you using?

I am currently running Streamlit 0.73.1

Kk, does adding the following before loading up the file with PyDICOM fix it?

dicom_bytes.seek(0)
dicom = pydicom.read_file(dicom_bytes, force=True)

For example:

Well… I no longer have the same error :sweat_smile:

This is when I try give the command:

files = st.file_uploader(‘select file to upload’)
files.seek(0)
dicom_structures = pydicom.read_file(files)
st.write(dicom_structures)

New error is usually a good error :slight_smile:

Is that happening on the st.write line?

I suspect streamlit will cope a lot better with the “json_dict format”:

https://pydicom.github.io/pydicom/stable/tutorials/dicom_json.html#converting-a-dataset-into-json-format

st.write(dicom_structures.to_json_dict())

would you be willing to try dcmread instead of read_file?

ds_input: pydicom.FileDataset = pydicom.dcmread(
                    uploaded_file_buffer, force=True
                )

from lib/pymedphys/_streamlit/apps/pseudonymise.py
if you want to see all the code (or maybe @SimonBiggs will show me how to provide the nice link).


[@SimonBiggs added link :slight_smile: ]

1 Like

This got it working, thank you so much!

1 Like