def get_image(path): image = PILImage.open(path) image = pad(image) img_array, meta_data = PILReader().get_data(image) img_array = img_array.astype(dtype=np.float32) img_array = img_array.transpose((2, 0, 1)) return transforms(img_array)#img_array,transforms(img_array)
def test_pil(self): tempdir = tempfile.mkdtemp() test_image = np.random.randint(0, 256, size=[128, 256]) filename = os.path.join(tempdir, "test_image.png") PILImage.fromarray(test_image.astype("uint8")).save(filename) loader = LoadImage(PILReader(converter=lambda image: image.convert("LA"))) _ = loader(filename)
def __init__( self, reader: Optional[Union[ImageReader, str]] = None, image_only: bool = False, dtype: DtypeLike = np.float32, *args, **kwargs, ) -> None: """ Args: reader: register reader to load image file and meta data, if None, still can register readers at runtime or use the default readers. If a string of reader name provided, will construct a reader object with the `*args` and `**kwargs` parameters, supported reader name: "NibabelReader", "PILReader", "ITKReader", "NumpyReader", "WSIReader". image_only: if True return only the image volume, otherwise return image data array and header dict. dtype: if not None convert the loaded image to this data type. args: additional parameters for reader if providing a reader name. kwargs: additional parameters for reader if providing a reader name. Note: The transform returns image data array if `image_only` is True, or a tuple of two elements containing the data array, and the meta data in a dict format otherwise. """ # set predefined readers as default self.readers: List[ImageReader] = [ ITKReader(), NumpyReader(), PILReader(), NibabelReader(), WSIReader() ] if reader is not None: if isinstance(reader, str): supported_readers = { "nibabelreader": NibabelReader, "pilreader": PILReader, "itkreader": ITKReader, "numpyreader": NumpyReader, "wsireader": WSIReader, } reader = reader.lower() if reader not in supported_readers: raise ValueError( f"unsupported reader type: {reader}, available options: {supported_readers}." ) self.register(supported_readers[reader](*args, **kwargs)) else: self.register(reader) self.image_only = image_only self.dtype = dtype