Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
how to read only metadata from a dicom url
how to read only metadata from a dicom url to save bandwith
- note the server must support HTTP Range, e.g. s3 buckets or azure blobs.
- note that if you don't mind reading the whole thing, it's easier to just read the whole thing, then pass it into pydicom as io.BytesIO
import httpio
import pydicom
class HTTPIOFile2(httpio.HTTPIOFile):
We want the open url to act like an open file for pidicom
httpio helps, but does not return an EOFError which pydicom waits for...
def seek(self, *args, **kwargs):
super().seek(*args, **kwargs)
except httpio.HTTPIOError as e:
if 'Invalid argument: cursor=' in e.args[0]:
raise EOFError
def dcmread_http(url, stop_before_pixels=True, force=True):
with HTTPIOFile2(url, block_size=1024) as r:
dcm = pydicom.dcmread(r, stop_before_pixels=stop_before_pixels, defer_size="2 KB", force=force)
return dcm
# test
url = ""
dcm = dcmread_http(url)
assert dcm is not None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment