def download_image()

in tcav/tcav_examples/image_models/imagenet/imagenet_and_broden_fetcher.py [0:0]


def download_image(path, url):
  image_name = url.split("/")[-1]
  image_name = image_name.split("?")[0]
  image_prefix = image_name.split(".")[0]
  saving_path = os.path.join(path, image_prefix + ".jpg")
  urllib.request.urlretrieve(url, saving_path)

  try:
    # Throw an exception if the image is unreadable or corrupted
    Image.open(saving_path).verify()

    # Remove images smaller than 10kb, to make sure we are not downloading empty/low quality images
    if tf.io.gfile.stat(saving_path).length < kMinFileSize:
      tf.io.gfile.remove(saving_path)
  # PIL.Image.verify() throws a default exception if it finds a corrupted image.
  except Exception as e:
    tf.io.gfile.remove(
        saving_path
    )  # We need to delete it, since urllib automatically saves them.
    raise e