class _FakeImg():
def __init__(self, size): self.size=size
= _FakeImg((200,500))
img 400), [160,400])
test_eq(resize_to(img, 400, use_min=True), [400,1000]) test_eq(resize_to(img,
Vision utils
Some utils function to quickly download a bunch of images, check them and pre-resize them
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
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)*
verify_image
verify_image (fn)
Confirm that fn
can be opened
verify_images
verify_images (fns)
Find images in fns
that can’t be opened
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'
= Path('.')
dest file, dest, src='images', max_size=400)
resize_image(= Image.open(dest/file)
im 1],400)
test_eq(im.shape[/file).unlink() (dest
file = 'puppy.jpg'
= Path('images')
dest file, dest, src=dest, max_size=None) resize_image(
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:
= Path(d)/'resized_images'
dest 'images', max_size=100, dest=dest, max_workers=0, recurse=True) resize_images(