Last active
April 10, 2021 06:24
-
-
Save wassname/70106b2d66a7c6e83e4b0300c9d1d4d3 to your computer and use it in GitHub Desktop.
how to read only metadata from a dicom url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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