Any function to detect images with "imagery unavailable"

Hi,

Is there a function available I can use to identify images with “imagery unavailable”? Or I can use the size of the image to exclude images, for example: delete images with less than 5KB.

Thank you!
Emily

Hi Emily,

A little more context would be helpful, but here is a very simple script for deleting image files from a directory that cannot be opened with PIL:

from fastai.vision.all import *
from pathlib import Path
path = Path('\my\image\file\path')
img_fnames = get_image_files(path)
for fname in img_fnames:
    try:
        Image.open(str(fname))
    except:
        print(f'Deleting {fname}')
        fname.unlink()

If you want to do it by file size you could do something like this. Modifying the file size (in bytes) to whatever size works best for your situation.

from fastai.vision.all import *
from pathlib import Path
import os
path = Path('\my\image\file\path')
img_fnames = get_image_files(path)
for fname in img_fnames:
    if os.path.getsize(fname) < 1000:
        print(f'Deleting {fname}')
        fname.unlink()