code: def _resize_image_if_necessary(image_fobj, target_pixels=None):
      """Resize an image to have (roughly) the given number of target pixels.
  
      Args:
        image_fobj: File object containing the original image.
        target_pixels: If given, number of pixels that the image must have.
  
      Returns:
        A file object.
      """
      if target_pixels is None:
          return image_fobj
  
      cv2 = tfds.core.lazy_imports.cv2
      # Decode image using OpenCV2.
      image = cv2.imdecode(
          np.frombuffer(image_fobj.read(), dtype=np.uint8), flags=3
      )
      # Get image height and width.
      height, width, _ = image.shape
      actual_pixels = height * width
      if actual_pixels > target_pixels:
          factor = np.sqrt(target_pixels / actual_pixels)
          image = cv2.resize(image, dsize=None, fx=factor, fy=factor)
      # Encode the image with quality=72 and store it in a BytesIO object.
      _, buff = cv2.imencode(
          ".jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 72]
      )
      return io.BytesIO(buff.tobytes())
  
imports: from __future__ import annotations
  import collections
  import csv
  import functools
  import io
  import os
  from typing import List
  from absl import logging
  from etils import epath
  import numpy
  import tensorflow_datasets.public_api
label_desc: Use image node_id 1 for calling a global function with example usage: _resize_image_if_necessary(image_fobj, target_pixels) and returns: io, image_fobj