This is very condensed code, so I try to break it down.
On a high level, this function iterates a list or tuple of filenames and tries to open them as images. It will return a
list of all image filenames (specifically, the enhanced list type
L), which could not be opened.
verify_image is called, which will try to open an image under the filename
fn. If the image can be opened, it will return
True; if the file can’t be opened, it will return
"Confirm that `fn` can be opened"
im = Image.open(fn) # uses PIL.Image to open an image at fn
except: return False
If you have an extensive list of filenames, this can take some time. Therefore, the function is applied in
def parallel(f, items, *args, n_workers=defaults.cpus, total=None, progress=None, pause=0,
threadpool=False, timeout=None, chunksize=1, **kwargs):
"Applies `func` in parallel to `items`, using `n_workers`"
parallel function enables the usage of multiple CPU cores for a computing task. So instead of having one core checking all filenames, multiple cores can be used, greatly speeding up the operation. The
parallel function will return a list with the output from the function applied in parallel. In this case, as the
verify_image function was applied, the output will be a list with
False in it.
The list is then
enumerated. This means the first item
o with its index
i is returned first, then the second item with its index, then the third …
If the image could be opened,
o will be true. If the image could not be opened,
o will be false.
In the last step, a list comprehension with an additional logical condition is applied. It does look unfamiliar as
L was used, which is an enhanced version of the python
list. But one could write it just as a classical list comprehension (but lose all benefits from the
[fns[i] for i,o in enumerate(parallel(verify_image, fns)) if not o]
False (image could not be opened), that means
not o is
if not o, the filename in the list at place
fns[i]) will be returned. As this is a list comprehension, the operation yields a list with all filenames, which could not be opened as an image.