(
    image_path = None,
    image_bytes = None,
    invert_img = False,
    bbox=None)
return image_bytes
Loads and processes an image.

  Args:
    image_path: image path. Exactly one of image_path or image_bytes should be
      passed.
    image_bytes: image bytes. Exactly one of image_path or image_bytes should be
      passed.
    invert_img: if True, invert the image.
    bbox: if passed, crop the image using the bounding box.

  Returns:
    Image bytes.
Loads and processes an image.
Loads and processes an image .
def _load_and_process_image(
    image_path = None,
    image_bytes = None,
    invert_img = False,
    bbox=None):
  """Loads and processes an image.

  Args:
    image_path: image path. Exactly one of image_path or image_bytes should be
      passed.
    image_bytes: image bytes. Exactly one of image_path or image_bytes should be
      passed.
    invert_img: if True, invert the image.
    bbox: if passed, crop the image using the bounding box.

  Returns:
    Image bytes.
  """
  if (None not in (image_path, image_bytes)) or not (image_path or image_bytes):
    raise ValueError(
        'exactly one of image_path and image_bytes should be passed.')
  if image_path is not None:
    image_bytes = image_path.read_bytes()
  try:
    img = tfds.core.lazy_imports.PIL_Image.open(io.BytesIO(image_bytes))
  except:
    logging.warn('Failed to open image')
    raise

  assert image_bytes is not None
  img_needs_encoding = False

  if img.format != 'JPEG':
    img_needs_encoding = True
  if img.mode != 'RGB':
    img = img.convert('RGB')
    img_needs_encoding = True
  if bbox is not None:
    img = img.crop(bbox)
    img_needs_encoding = True
  if invert_img:
    img = ImageOps.invert(img)
    img_needs_encoding = True

  if img_needs_encoding:
    # Convert the image into JPEG
    buf = io.BytesIO()
    img.save(buf, format='JPEG')
    buf.seek(0)
    image_bytes = buf.getvalue()
  return image_bytes
def _load_and_process_image ( image_path = None , image_bytes = None , invert_img = False , bbox = None ) : if ( None not in ( image_path , image_bytes ) ) or not ( image_path or image_bytes ) : raise ValueError ( 'exactly one of image_path and image_bytes should be passed.' ) if image_path is not None : image_bytes = image_path . read_bytes ( ) try : img = tfds . core . lazy_imports . PIL_Image . open ( io . BytesIO ( image_bytes ) ) except : logging . warn ( 'Failed to open image' ) raise assert image_bytes is not None img_needs_encoding = False if img . format != 'JPEG' : img_needs_encoding = True if img . mode != 'RGB' : img = img . convert ( 'RGB' ) img_needs_encoding = True if bbox is not None : img = img . crop ( bbox ) img_needs_encoding = True if invert_img : img = ImageOps . invert ( img ) img_needs_encoding = True if img_needs_encoding : # Convert the image into JPEG buf = io . BytesIO ( ) img . save ( buf , format = 'JPEG' ) buf . seek ( 0 ) image_bytes = buf . getvalue ( ) return image_bytes