code: def generate_tfrecords_from_images(
      num_images: int,
      sizes: List[Tuple[int, int]],
      modes: List[str],
      formats: List[str],
  ) -> str:
      images_dir = generate_images(num_images, sizes, modes, formats)
      try:
          ds = ray.data.read_images(images_dir)
  
          # Convert images from NumPy to bytes
          def images_to_bytes(batch):
              return {
                  "image": [image.tobytes() for image in batch["image"]]
              }
  
          ds = ds.map_batches(images_to_bytes, batch_format="numpy")
          assert ds.count() == num_images
  
          tfrecords_dir = tempfile.mkdtemp()
          ds.write_tfrecords(tfrecords_dir)
      finally:
          shutil.rmtree(images_dir)
      return tfrecords_dir
  
file_path: ray/release/nightly_tests/dataset/read_tfrecords_benchmark.py
imports: import random
  import shutil
  import tempfile
  from typing import List, Tuple
  import ray
  from ray.data.dataset import Dataset
  from benchmark import Benchmark
  from read_images_benchmark import generate_images
  import pyarrow
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: generate_tfrecords_from_images(num_images, sizes, modes, formats) and returns: tfrecords_dir, dict