code: def test_array_to_img_and_img_to_array():
      height, width = 10, 8
  
      # Test the data format
      # Test RGB 3D
      x = np.random.random((3, height, width))
      img = utils.array_to_img(x, data_format="channels_first")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_first")
      assert x.shape == (3, height, width)
  
      # Test RGBA 3D
      x = np.random.random((4, height, width))
      img = utils.array_to_img(x, data_format="channels_first")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_first")
      assert x.shape == (4, height, width)
  
      # Test 2D
      x = np.random.random((1, height, width))
      img = utils.array_to_img(x, data_format="channels_first")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_first")
      assert x.shape == (1, height, width)
  
      # grayscale 32-bit signed integer
      x = np.array(
          np.random.randint(
              -2147483648, 2147483647, (1, height, width)
          ),
          dtype=np.int32,
      )
      img = utils.array_to_img(x, data_format="channels_first")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_first")
      assert x.shape == (1, height, width)
  
      # Test tf data format
      # Test RGB 3D
      x = np.random.random((height, width, 3))
      img = utils.array_to_img(x, data_format="channels_last")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_last")
      assert x.shape == (height, width, 3)
  
      # Test RGBA 3D
      x = np.random.random((height, width, 4))
      img = utils.array_to_img(x, data_format="channels_last")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_last")
      assert x.shape == (height, width, 4)
  
      # Test 2D
      x = np.random.random((height, width, 1))
      img = utils.array_to_img(x, data_format="channels_last")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_last")
      assert x.shape == (height, width, 1)
  
      # grayscale 16-bit signed integer
      x = np.array(
          np.random.randint(
              -2147483648, 2147483647, (height, width, 1)
          ),
          dtype=np.int16,
      )
      img = utils.array_to_img(x, data_format="channels_last")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_last")
      assert x.shape == (height, width, 1)
  
      # grayscale 32-bit signed integer
      x = np.array(
          np.random.randint(
              -2147483648, 2147483647, (height, width, 1)
          ),
          dtype=np.int32,
      )
      img = utils.array_to_img(x, data_format="channels_last")
      assert img.size == (width, height)
  
      x = utils.img_to_array(img, data_format="channels_last")
      assert x.shape == (height, width, 1)
  
      # Test invalid use case
      with pytest.raises(ValueError):
          x = np.random.random((height, width))  # not 3D
          img = utils.array_to_img(x, data_format="channels_first")
  
      with pytest.raises(ValueError):
          x = np.random.random((height, width, 3))
          # unknown data_format
          img = utils.array_to_img(x, data_format="channels")
  
      with pytest.raises(ValueError):
          # neither RGB, RGBA, or gray-scale
          x = np.random.random((height, width, 5))
          img = utils.array_to_img(x, data_format="channels_last")
  
      with pytest.raises(ValueError):
          x = np.random.random((height, width, 3))
          # unknown data_format
          img = utils.img_to_array(x, data_format="channels")
  
      with pytest.raises(ValueError):
          # neither RGB, RGBA, or gray-scale
          x = np.random.random((height, width, 5, 3))
          img = utils.img_to_array(x, data_format="channels_last")
  
file_path: keras-preprocessing/tests/image/utils_test.py
imports: import io
  import resource
  from pathlib import Path
  import numpy
  import PIL
  import pytest
  from keras_preprocessing.image import utils
label_desc: Use image node_id 4 for calling a global function with example usage: test_array_to_img_and_img_to_array() without return types