wpsrt/wpsrt/wallpapers.py

23 lines
722 B
Python
Raw Permalink Normal View History

2022-05-30 09:12:38 +02:00
import os
from pathlib import Path
from typing import Iterable, Tuple
2022-05-30 08:13:47 +02:00
2023-01-08 09:58:10 +01:00
from PIL import Image, UnidentifiedImageError
2022-05-30 08:13:47 +02:00
2022-05-30 09:12:38 +02:00
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():
2023-01-08 09:58:10 +01:00
try:
image = Image.open(filename)
yield (filename, image.size)
except UnidentifiedImageError:
continue
2022-05-30 09:12:38 +02:00
def move_wallpaper(wallpaper: Path, target: Path) -> Path:
if not target.parent.exists():
target.parent.mkdir()
return wallpaper.rename(target)