def test_get_image_content_from_figure(self): # given pyplot.plot([1, 2, 3, 4]) pyplot.ylabel("some interesting numbers") fig = pyplot.gcf() # expect self.assertEqual(get_image_content(fig), self._encode_figure(fig))
def test_get_image_content_from_pil_image(self): # given image_array = self._random_image_array() expected_image = Image.fromarray(image_array.astype(numpy.uint8)) # expect self.assertEqual( get_image_content(expected_image), self._encode_pil_image(expected_image) )
def test_get_image_content_from_2d_grayscale_array(self): # given image_array = self._random_image_array(d=None) scaled_array = image_array * 255 expected_image = Image.fromarray(scaled_array.astype(numpy.uint8)) # expect self.assertEqual( get_image_content(image_array), self._encode_pil_image(expected_image) )
def test_get_image_content_from_rgb_array(self): # given image_array = self._random_image_array() scaled_array = image_array * 255 expected_image = Image.fromarray(scaled_array.astype(numpy.uint8)) # expect self.assertEqual( get_image_content(image_array), self._encode_pil_image(expected_image) ) # and make sure that original image's size was preserved self.assertFalse((image_array * 255 - scaled_array).any())
def test_get_image_content_from_tensorflow_tensor(self): import tensorflow as tf # pylint: disable=C0415 # given # pylint: disable=E1120 # false positive image_tensor = tf.random.uniform(shape=[200, 300, 3]) expected_array = image_tensor.numpy() * 255 expected_image = Image.fromarray(expected_array.astype(numpy.uint8)) # expect self.assertEqual( get_image_content(image_tensor), self._encode_pil_image(expected_image) )
def test_get_image_content_from_torch_tensor(self): import torch # pylint: disable=C0415 # given image_tensor = torch.rand(200, 300, 3) # pylint: disable=no-member expected_array = image_tensor.numpy() * 255 expected_image = Image.fromarray(expected_array.astype(numpy.uint8)) # expect self.assertEqual( get_image_content(image_tensor), self._encode_pil_image(expected_image) ) # and make sure that original image's size was preserved self.assertFalse((image_tensor.numpy() * 255 - expected_array).any())
def test_get_image_content_from_3d_grayscale_array(self): # given image_array = numpy.array([[[1], [0]], [[-3], [4]], [[5], [6]]]) expected_array = numpy.array([[1, 0], [-3, 4], [5, 6]]) * 255 expected_image = Image.fromarray(expected_array.astype(numpy.uint8)) # expect stderr = io.StringIO() with contextlib.redirect_stderr(stderr): self.assertEqual( get_image_content(image_array), self._encode_pil_image(expected_image) ) self.assertEqual( stderr.getvalue(), "The smallest value in the array is -3 and the largest value in the array is 6." " To be interpreted as colors correctly values in the array need to be in the [0, 1] range.\n", )
def as_image(image) -> "File": """Static method for converting image objects or image-like objects to an image File value object. This way you can upload `Matplotlib` figures, `PIL` images, `NumPy` arrays, as static images. Args: image: Image-like object to be converted. Supported are `PyTorch` tensors, `TensorFlow/Keras` tensors, `NumPy` arrays, `PIL` images and `Matplotlib` figures. Returns: ``File``: value object with converted image Examples: >>> import neptune.new as neptune >>> from neptune.new.types import File >>> run = neptune.init() Convert NumPy array to File value object and upload it >>> run["train/prediction_example"].upload(File.as_image(numpy_array)) Convert PIL image to File value object and upload it >>> pil_file = File.as_image(pil_image) >>> run["dataset/data_sample/img1"].upload(pil_file) You can upload PIL image without explicit conversion >>> run["dataset/data_sample/img2"].upload(pil_image) You may also want to check `as_image docs page`_. .. _as_image docs page: https://docs.neptune.ai/api-reference/field-types#.as_image """ content_bytes = get_image_content(image) return File.from_content( content_bytes if content_bytes is not None else b"", extension="png")