MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / PILReader

Class PILReader

monai/data/image_reader.py:1344–1445  ·  view source on GitHub ↗

Load common 2D image format (supports PNG, JPG, BMP) file or files from provided path. Args: converter: additional function to convert the image data after `read()`. for example, use `converter=lambda image: image.convert("LA")` to convert image format. reverse_

Source from the content-addressed store, hash-verified

1342
1343@require_pkg(pkg_name="PIL")
1344class PILReader(ImageReader):
1345 """
1346 Load common 2D image format (supports PNG, JPG, BMP) file or files from provided path.
1347
1348 Args:
1349 converter: additional function to convert the image data after `read()`.
1350 for example, use `converter=lambda image: image.convert("LA")` to convert image format.
1351 reverse_indexing: whether to swap axis 0 and 1 after loading the array, this is enabled by default,
1352 so that output of the reader is consistent with the other readers. Set this option to ``False`` to use
1353 the PIL backend's original spatial axes convention.
1354 kwargs: additional args for `Image.open` API in `read()`, mode details about available args:
1355 https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open
1356 """
1357
1358 def __init__(self, converter: Callable | None = None, reverse_indexing: bool = True, **kwargs):
1359 super().__init__()
1360 self.converter = converter
1361 self.reverse_indexing = reverse_indexing
1362 self.kwargs = kwargs
1363
1364 def verify_suffix(self, filename: Sequence[PathLike] | PathLike) -> bool:
1365 """
1366 Verify whether the specified file or files format is supported by PIL reader.
1367
1368 Args:
1369 filename: file name or a list of file names to read.
1370 if a list of files, verify all the suffixes.
1371 """
1372 suffixes: Sequence[str] = ["png", "jpg", "jpeg", "bmp"]
1373 return has_pil and is_supported_format(filename, suffixes)
1374
1375 def read(self, data: Sequence[PathLike] | PathLike | np.ndarray, **kwargs):
1376 """
1377 Read image data from specified file or files, it can read a list of images
1378 and stack them together as multi-channel data in `get_data()`.
1379 Note that the returned object is PIL image or list of PIL image.
1380
1381 Args:
1382 data: file name or a list of file names to read.
1383 kwargs: additional args for `Image.open` API in `read()`, will override `self.kwargs` for existing keys.
1384 Mode details about available args:
1385 https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open
1386
1387 """
1388 img_: list[PILImage.Image] = []
1389
1390 filenames: Sequence[PathLike] = ensure_tuple(data)
1391 kwargs_ = self.kwargs.copy()
1392 kwargs_.update(kwargs)
1393 for name in filenames:
1394 img = PILImage.open(name, **kwargs_)
1395 if callable(self.converter):
1396 img = self.converter(img)
1397 img_.append(img)
1398
1399 return img_ if len(filenames) > 1 else img_[0]
1400
1401 def get_data(self, img) -> tuple[np.ndarray, dict]:

Callers 4

test_shape_valueMethod · 0.90
test_converterMethod · 0.90
test_readersMethod · 0.90
test_pilMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_shape_valueMethod · 0.72
test_converterMethod · 0.72
test_readersMethod · 0.72
test_pilMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…