code: def test_load_img(tmpdir):
      filename_rgb = str(tmpdir / "rgb_utils.png")
      filename_rgba = str(tmpdir / "rgba_utils.png")
      filename_grayscale_8bit = str(tmpdir / "grayscale_8bit_utils.png")
      filename_grayscale_16bit = str(
          tmpdir / "grayscale_16bit_utils.tiff"
      )
      filename_grayscale_32bit = str(
          tmpdir / "grayscale_32bit_utils.tiff"
      )
  
      original_rgb_array = np.array(
          255 * np.random.rand(100, 100, 3), dtype=np.uint8
      )
      original_rgb = utils.array_to_img(original_rgb_array, scale=False)
      original_rgb.save(filename_rgb)
  
      original_rgba_array = np.array(
          255 * np.random.rand(100, 100, 4), dtype=np.uint8
      )
      original_rgba = utils.array_to_img(
          original_rgba_array, scale=False
      )
      original_rgba.save(filename_rgba)
  
      original_grayscale_8bit_array = np.array(
          255 * np.random.rand(100, 100, 1), dtype=np.uint8
      )
      original_grayscale_8bit = utils.array_to_img(
          original_grayscale_8bit_array, scale=False
      )
      original_grayscale_8bit.save(filename_grayscale_8bit)
  
      original_grayscale_16bit_array = np.array(
          np.random.randint(-2147483648, 2147483647, (100, 100, 1)),
          dtype=np.int16,
      )
      original_grayscale_16bit = utils.array_to_img(
          original_grayscale_16bit_array, scale=False, dtype="int16"
      )
      original_grayscale_16bit.save(filename_grayscale_16bit)
  
      original_grayscale_32bit_array = np.array(
          np.random.randint(-2147483648, 2147483647, (100, 100, 1)),
          dtype=np.int32,
      )
      original_grayscale_32bit = utils.array_to_img(
          original_grayscale_32bit_array, scale=False, dtype="int32"
      )
      original_grayscale_32bit.save(filename_grayscale_32bit)
  
      # Test that loaded image is exactly equal to original.
  
      loaded_im = utils.load_img(filename_rgb)
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == original_rgb_array.shape
      assert np.all(loaded_im_array == original_rgb_array)
  
      loaded_im = utils.load_img(filename_rgba, color_mode="rgba")
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == original_rgba_array.shape
      assert np.all(loaded_im_array == original_rgba_array)
  
      loaded_im = utils.load_img(filename_rgb, color_mode="grayscale")
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (
          original_rgb_array.shape[0],
          original_rgb_array.shape[1],
          1,
      )
  
      loaded_im = utils.load_img(
          filename_grayscale_8bit, color_mode="grayscale"
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert (
          loaded_im_array.shape == original_grayscale_8bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_8bit_array)
  
      loaded_im = utils.load_img(
          filename_grayscale_16bit, color_mode="grayscale"
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int16")
      assert (
          loaded_im_array.shape == original_grayscale_16bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_16bit_array)
      # test casting int16 image to float32
      loaded_im_array = utils.img_to_array(loaded_im)
      assert np.allclose(
          loaded_im_array, original_grayscale_16bit_array
      )
  
      loaded_im = utils.load_img(
          filename_grayscale_32bit, color_mode="grayscale"
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int32")
      assert (
          loaded_im_array.shape == original_grayscale_32bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
      # test casting int32 image to float32
      loaded_im_array = utils.img_to_array(loaded_im)
      assert np.allclose(
          loaded_im_array, original_grayscale_32bit_array
      )
  
      # Test that nothing is changed when target size is equal to original.
  
      loaded_im = utils.load_img(filename_rgb, target_size=(100, 100))
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == original_rgb_array.shape
      assert np.all(loaded_im_array == original_rgb_array)
  
      loaded_im = utils.load_img(
          filename_rgba, color_mode="rgba", target_size=(100, 100)
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == original_rgba_array.shape
      assert np.all(loaded_im_array == original_rgba_array)
  
      loaded_im = utils.load_img(
          filename_rgb, color_mode="grayscale", target_size=(100, 100)
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (
          original_rgba_array.shape[0],
          original_rgba_array.shape[1],
          1,
      )
  
      loaded_im = utils.load_img(
          filename_grayscale_8bit,
          color_mode="grayscale",
          target_size=(100, 100),
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert (
          loaded_im_array.shape == original_grayscale_8bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_8bit_array)
  
      loaded_im = utils.load_img(
          filename_grayscale_16bit,
          color_mode="grayscale",
          target_size=(100, 100),
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int16")
      assert (
          loaded_im_array.shape == original_grayscale_16bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_16bit_array)
  
      loaded_im = utils.load_img(
          filename_grayscale_32bit,
          color_mode="grayscale",
          target_size=(100, 100),
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int32")
      assert (
          loaded_im_array.shape == original_grayscale_32bit_array.shape
      )
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
  
      # Test down-sampling with bilinear interpolation.
  
      loaded_im = utils.load_img(filename_rgb, target_size=(25, 25))
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 3)
  
      loaded_im = utils.load_img(
          filename_rgba, color_mode="rgba", target_size=(25, 25)
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 4)
  
      loaded_im = utils.load_img(
          filename_rgb, color_mode="grayscale", target_size=(25, 25)
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 1)
  
      loaded_im = utils.load_img(
          filename_grayscale_8bit,
          color_mode="grayscale",
          target_size=(25, 25),
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 1)
  
      loaded_im = utils.load_img(
          filename_grayscale_16bit,
          color_mode="grayscale",
          target_size=(25, 25),
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int16")
      assert loaded_im_array.shape == (25, 25, 1)
  
      loaded_im = utils.load_img(
          filename_grayscale_32bit,
          color_mode="grayscale",
          target_size=(25, 25),
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int32")
      assert loaded_im_array.shape == (25, 25, 1)
  
      # Test down-sampling with nearest neighbor interpolation.
  
      loaded_im_nearest = utils.load_img(
          filename_rgb, target_size=(25, 25), interpolation="nearest"
      )
      loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest)
      assert loaded_im_array_nearest.shape == (25, 25, 3)
      assert np.any(loaded_im_array_nearest != loaded_im_array)
  
      loaded_im_nearest = utils.load_img(
          filename_rgba,
          color_mode="rgba",
          target_size=(25, 25),
          interpolation="nearest",
      )
      loaded_im_array_nearest = utils.img_to_array(loaded_im_nearest)
      assert loaded_im_array_nearest.shape == (25, 25, 4)
      assert np.any(loaded_im_array_nearest != loaded_im_array)
  
      loaded_im = utils.load_img(
          filename_grayscale_8bit,
          color_mode="grayscale",
          target_size=(25, 25),
          interpolation="nearest",
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 1)
  
      loaded_im = utils.load_img(
          filename_grayscale_16bit,
          color_mode="grayscale",
          target_size=(25, 25),
          interpolation="nearest",
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int16")
      assert loaded_im_array.shape == (25, 25, 1)
  
      loaded_im = utils.load_img(
          filename_grayscale_32bit,
          color_mode="grayscale",
          target_size=(25, 25),
          interpolation="nearest",
      )
      loaded_im_array = utils.img_to_array(loaded_im, dtype="int32")
      assert loaded_im_array.shape == (25, 25, 1)
  
      # Test different path type
      with open(filename_grayscale_32bit, "rb") as f:
          _path = io.BytesIO(f.read())  # io.Bytesio
      loaded_im = utils.load_img(_path, color_mode="grayscale")
      loaded_im_array = utils.img_to_array(loaded_im, dtype=np.int32)
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
  
      _path = filename_grayscale_32bit  # str
      loaded_im = utils.load_img(_path, color_mode="grayscale")
      loaded_im_array = utils.img_to_array(loaded_im, dtype=np.int32)
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
  
      _path = filename_grayscale_32bit.encode()  # bytes
      loaded_im = utils.load_img(_path, color_mode="grayscale")
      loaded_im_array = utils.img_to_array(loaded_im, dtype=np.int32)
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
  
      _path = Path(tmpdir / "grayscale_32bit_utils.tiff")  # Path
      loaded_im = utils.load_img(_path, color_mode="grayscale")
      loaded_im_array = utils.img_to_array(loaded_im, dtype=np.int32)
      assert np.all(loaded_im_array == original_grayscale_32bit_array)
  
      # Check that exception is raised if interpolation not supported.
  
      loaded_im = utils.load_img(
          filename_rgb, interpolation="unsupported"
      )
      with pytest.raises(ValueError):
          loaded_im = utils.load_img(
              filename_rgb,
              target_size=(25, 25),
              interpolation="unsupported",
          )
  
      # Check that the aspect ratio of a square is the same
  
      filename_red_square = str(tmpdir / "red_square_utils.png")
      A = np.zeros(
          (50, 100, 3), dtype=np.uint8
      )  # rectangle image 100x50
      A[20:30, 45:55, 0] = 255  # red square 10x10
      red_square_array = np.array(A)
      red_square = utils.array_to_img(red_square_array, scale=False)
      red_square.save(filename_red_square)
  
      loaded_im = utils.load_img(
          filename_red_square,
          target_size=(25, 25),
          keep_aspect_ratio=True,
      )
      loaded_im_array = utils.img_to_array(loaded_im)
      assert loaded_im_array.shape == (25, 25, 3)
  
      red_channel_arr = loaded_im_array[:, :, 0].astype(np.bool)
      square_width = np.sum(np.sum(red_channel_arr, axis=0))
      square_height = np.sum(np.sum(red_channel_arr, axis=1))
      aspect_ratio_result = square_width / square_height
  
      # original square had 1:1 ratio
      assert aspect_ratio_result == pytest.approx(1.0)
  
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 2 for calling a global function with example usage: test_load_img(tmpdir) without return types