Vision utils

Some utils function to quickly download a bunch of images, check them and pre-resize them

source

download_images

 download_images (dest, url_file=None, urls=None, max_pics=1000,
                  n_workers=8, timeout=4, preserve_filename=False)

Download images listed in text file url_file to path dest, at most max_pics


source

resize_to

 resize_to (img, targ_sz, use_min=False)

Size to resize to, to hit targ_sz at same aspect ratio, in PIL coords (i.e wh)*

class _FakeImg():
    def __init__(self, size): self.size=size

img = _FakeImg((200,500))
test_eq(resize_to(img, 400), [160,400])
test_eq(resize_to(img, 400, use_min=True), [400,1000])

source

verify_image

 verify_image (fn)

Confirm that fn can be opened


source

verify_images

 verify_images (fns)

Find images in fns that can’t be opened


source

resize_image

 resize_image (file, dest, src='.', max_size=None, n_channels=3, ext=None,
               img_format=None, resample=<Resampling.BILINEAR: 2>,
               resume=False, **kwargs)

Resize file to dest to max_size

file = 'puppy.jpg'
dest = Path('.')
resize_image(file, dest, src='images', max_size=400)
im = Image.open(dest/file)
test_eq(im.shape[1],400)
(dest/file).unlink()
file = 'puppy.jpg'
dest = Path('images')
resize_image(file, dest, src=dest, max_size=None)

source

resize_images

 resize_images (path, max_workers=4, max_size=None, recurse=False,
                dest=Path('.'), n_channels=3, ext=None, img_format=None,
                resample=<Resampling.BILINEAR: 2>, resume=None, **kwargs)

Resize files on path recursively to dest to max_size

with tempfile.TemporaryDirectory() as d:
    dest = Path(d)/'resized_images'
    resize_images('images', max_size=100, dest=dest, max_workers=0, recurse=True)