def __init__(self, data_or_path, mode=None, caption=None, grouping=None): """ Accepts numpy array of image data, or a PIL image. The class attempts to infer the data format and converts it. If grouping is set to a number the interface combines N images. """ self._grouping = grouping self._caption = caption self._width = None self._height = None self._image = None if isinstance(data_or_path, six.string_types): super(Image, self).__init__(data_or_path, is_tmp=False) else: data = data_or_path PILImage = util.get_module( "PIL.Image", required= 'wandb.Image needs the PIL package. To get it, run "pip install pillow".' ) if util.is_matplotlib_typename(util.get_full_typename(data)): buf = six.BytesIO() util.ensure_matplotlib_figure(data).savefig(buf) self._image = PILImage.open(buf) elif isinstance(data, PILImage.Image): self._image = data elif util.is_pytorch_tensor_typename(util.get_full_typename(data)): vis_util = util.get_module( "torchvision.utils", "torchvision is required to render images") if hasattr(data, "requires_grad") and data.requires_grad: data = data.detach() data = vis_util.make_grid(data, normalize=True) self._image = PILImage.fromarray( data.mul(255).clamp(0, 255).byte().permute(1, 2, 0).cpu().numpy()) else: if hasattr(data, "numpy"): # TF data eager tensors data = data.numpy() if data.ndim > 2: data = data.squeeze( ) # get rid of trivial dimensions as a convenience self._image = PILImage.fromarray(self.to_uint8(data), mode=mode or self.guess_mode(data)) self._width, self._height = self._image.size tmp_path = os.path.join(MEDIA_TMP.name, util.generate_id() + '.png') self._image.save(tmp_path, transparency=None) super(Image, self).__init__(tmp_path, is_tmp=True)
def __init__(self, data, mode=None, caption=None, grouping=None): """ Accepts numpy array of image data, or a PIL image. The class attempts to infer the data format and converts it. If grouping is set to a number the interface combines N images. """ PILImage = util.get_module( "PIL.Image", required= "wandb.Image requires the PIL package, to get it run: pip install pillow" ) if util.is_matplotlib_typename(util.get_full_typename(data)): buf = six.BytesIO() util.ensure_matplotlib_figure(data).savefig(buf) self.image = PILImage.open(buf) elif isinstance(data, PILImage.Image): self.image = data elif util.is_pytorch_tensor_typename(util.get_full_typename(data)): vis_util = util.get_module( "torchvision.utils", "torchvision is required to render images") if hasattr(data, "requires_grad") and data.requires_grad: data = data.detach() data = vis_util.make_grid(data, normalize=True) self.image = PILImage.fromarray( data.mul(255).clamp(0, 255).byte().permute(1, 2, 0).cpu().numpy()) else: # Handle TF eager tensors if hasattr(data, "numpy"): data = data.numpy() data = data.squeeze( ) # get rid of trivial dimensions as a convenience self.image = PILImage.fromarray(self.to_uint8(data), mode=mode or self.guess_mode(data)) self.grouping = grouping self.caption = caption