code: def test_image_custom_decode(self):
      # Do not uses random here because Jpeg compression has loss, so decoded
      # value isn't the same
      img_shaped = np.ones(shape=(30, 60, 3), dtype=np.uint8)
      x, y, w, h = 4, 7, 10, 13
      img_cropped = img_shaped[y : y + h, x : x + w, :]
  
      class DecodeCrop(decode_lib.Decoder):
          """Simple class on how to customize the decoding."""
  
          def decode_example(self, serialized_image):
              return tf.image.decode_and_crop_jpeg(
                  serialized_image,
                  [y, x, h, w],
                  channels=self.feature.shape[-1],
              )
  
      @decode_lib.make_decoder()
      def decode_crop(serialized_image, feature):
          return tf.image.decode_and_crop_jpeg(
              serialized_image,
              [y, x, h, w],
              channels=feature.shape[-1],
          )
  
      image_path = os.fspath(
          utils.tfds_path("testing/test_data/test_image.jpg")
      )
      with tf.io.gfile.GFile(image_path, "rb") as f:
          serialized_img = f.read()
  
      self.assertFeature(
          # Image with statically defined shape
          feature=features_lib.Image(
              shape=(30, 60, 3), encoding_format="jpeg"
          ),
          shape=(30, 60, 3),
          dtype=np.uint8,
          # Output shape is different.
          test_tensor_spec=False,
          tests=[
              testing.FeatureExpectationItem(
                  value=img_shaped,
                  expected=img_cropped,
                  shape=(13, 10, 3),  # Shape is cropped
                  decoders=DecodeCrop(),
              ),
              testing.FeatureExpectationItem(
                  value=img_shaped,
                  expected=img_cropped,
                  shape=(13, 10, 3),  # Shape is cropped
                  decoders=decode_crop(),  # pylint: disable=no-value-for-parameter
              ),
              testing.FeatureExpectationItem(
                  value=image_path,
                  expected=serialized_img,
                  shape=(),
                  dtype=np.str_,
                  decoders=decode_lib.SkipDecoding(),
              ),
          ],
      )
  
file_path: datasets/tensorflow_datasets/core/decode/base_test.py
imports: import os
  import numpy
  import tensorflow
  from tensorflow_datasets import testing
  from tensorflow_datasets.core import decode
  from tensorflow_datasets.core import features
  from tensorflow_datasets.core import utils
label_desc: Use image node_id 1 for calling the BaseDecodeTest obj's underlying member method code with example usage: obj.test_image_custom_decode() and returns: tf, tf