def test_get_image_content_from_rgba_array(self): # given image_array = numpy.random.rand(200, 300, 4)*255 expected_image = Image.fromarray(image_array.astype(numpy.uint8)) # expect self.assertEqual(get_image_content(image_array), _get_pil_image_data(expected_image))
def test_get_image_content_from_3d_grayscale_array(self): # given image_array = numpy.array([[[1], [2]], [[3], [4]], [[5], [6]]]) expected_array = numpy.array([[1, 2], [3, 4], [5, 6]]) * 255 expected_image = Image.fromarray(expected_array.astype(numpy.uint8)) # expect self.assertEqual(get_image_content(image_array), _get_pil_image_data(expected_image))
def test_get_image_content_from_string(self): # given filename = "{}/image.png".format(self.TEST_DIR) image_array = numpy.random.rand(200, 300, 3)*255 expected_image = Image.fromarray(image_array.astype(numpy.uint8)) expected_image.save(filename) # expect self.assertEqual(get_image_content(filename), _get_pil_image_data(expected_image))
def test_get_image_content_from_tensorflow_tensor(self): import tensorflow as tf # pylint: disable=C0415 # given 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), _get_pil_image_data(expected_image))
def test_get_image_content_from_torch_tensor(self): import torch # pylint: disable=C0415 # given image_tensor = torch.rand(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), _get_pil_image_data(expected_image))
def test_get_image_content_from_figure(self): # given pyplot.plot([1, 2, 3, 4]) pyplot.ylabel('some interesting numbers') figure = pyplot.gcf() figure.canvas.draw() expected_image = Image.frombytes('RGB', figure.canvas.get_width_height(), figure.canvas.tostring_rgb()) # expect self.assertEqual(get_image_content(figure), _get_pil_image_data(expected_image))