Seemingly unsuppressible warning printed to stderr by DICOM reader
When reading the prostate DICOM example image from vtk/vtk-examples using the Python API the read is successful but a warning is emitted (twice):
from vtkmodules.vtkIOImage import vtkDICOMImageReader
path = 'prostate.img' # from https://gitlab.kitware.com/vtk/vtk-examples/-/blob/aa1716a165150f338f6182ce1ac174334f3b9003/src/Testing/Data/prostate.img
reader = vtkDICOMImageReader()
reader.SetFileName(path)
reader.Update() # <-- prints warning
img = reader.GetOutput()
The warning says
No DICOM magic number found, but file appears to be DICOM.
Proceeding without caution.
No DICOM magic number found, but file appears to be DICOM.
Proceeding without caution.
Redirecting stderr of the whole Python process in bash catches this warning, so it seems to be on stderr. However redirection attempts of Python's stderr within Python seem not to affect the warning, e.g.
from contextlib import redirect_stderr
import os
import sys
from vtkmodules.vtkIOImage import vtkDICOMImageReader
path = 'prostate.img'
with open(os.devnull, 'w') as devnull:
with redirect_stderr(devnull):
print('This gets suppressed nicely', file=sys.stderr)
reader = vtkDICOMImageReader()
reader.SetFileName(path)
reader.Update()
img = reader.GetOutput()
The manual print to sys.stderr
gets suppressed as expected. The DICOM warning remains. If I switch to redirect_stdout()
(as a sanity check) then both the print and the warning remain, as expected.
Is this resilience to suppression expected behaviour, and is there a way to work around it?