code: def _resize_image_if_necessary(image_fobj, target_pixels=None):
    
    if target_pixels is None:
      return image_fobj
  
    cv2 = tfds.core.lazy_imports.cv2
    # Decode image using OpenCV2.
    image = cv2.imdecode(
        np.fromstring(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.tostring())
docs: 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.