mindspore.dataset.vision.ToPIL
- class mindspore.dataset.vision.ToPIL[源代码]
将 numpy.ndarray 格式的解码图像转换为 PIL.Image.Image 格式的图像。
- 异常:
TypeError - 当输入图像的类型不为
numpy.ndarray
或 PIL.Image.Image 。
- 支持平台:
CPU
样例:
>>> import numpy as np >>> import mindspore.dataset as ds >>> import mindspore.dataset.vision as vision >>> from mindspore.dataset.transforms import Compose >>> >>> # Use the transform in dataset pipeline mode >>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8) >>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"]) >>> # data is already decoded, but not in PIL Image format >>> transforms_list = Compose([vision.ToPIL(), ... vision.RandomHorizontalFlip(0.5), ... vision.ToTensor()]) >>> # apply the transform to dataset through map function >>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns="image") >>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["image"].shape, item["image"].dtype) ... break (3, 100, 100) float32 >>> >>> # Use the transform in eager mode >>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8) >>> output = vision.ToPIL()(data) >>> print(type(output), np.array(output).shape, np.array(output).dtype) <class 'PIL.Image.Image'> (100, 100, 3) uint8
- 教程样例: