Skip to content

Instantly share code, notes, and snippets.

Embed
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
url: https://gist.github.com/wassname/70106b2d66a7c6e83e4b0300c9d1d4d3
"""
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):
try:
super().seek(*args, **kwargs)
except httpio.HTTPIOError as e:
if 'Invalid argument: cursor=' in e.args[0]:
raise EOFError
else:
raise
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
print(dcm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment