Handle non image files safely

This commit is contained in:
jpk 2023-01-08 09:58:10 +01:00
parent beb2de46d1
commit 987a99c8a1
1 changed files with 6 additions and 3 deletions

View File

@ -2,15 +2,18 @@ import os
from pathlib import Path
from typing import Iterable, Tuple
from PIL import Image
from PIL import Image, UnidentifiedImageError
def scan_directory(directory: Path) -> Iterable[Tuple[Path, Tuple[int, int]]]:
for root, _, filenames in os.walk(directory):
for filename in [Path(os.path.join(root, fname)) for fname in filenames]:
if filename.is_file():
image = Image.open(filename)
yield (filename, image.size)
try:
image = Image.open(filename)
yield (filename, image.size)
except UnidentifiedImageError:
continue
def move_wallpaper(wallpaper: Path, target: Path) -> Path: