Script Python pour afficher toutes images dossier (PIL/Pillow)
Voici un script Python propre, multiplateforme, utilisant Pillow (PIL) pour ouvrir et afficher chaque JPEG/PNG dans le répertoire courant. Marche Windows, macOS, Linux.
Prérequis
pip install pillow
Script : view_images.py
#!/usr/bin/env python3
""" Affiche tous JPEG/PNG images dans répertoire travail courant via Pillow.
Chaque image ouvre dans visualiseur OS défaut (ou Pillow .show() intégré).
Appuyez Entrée dans terminal pour passer image suivante. """
from pathlib import Path
from PIL import Image
import sys
def main():
cwd = Path.cwd()
# Extensions supportées (insensible casse)
exts = {'.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG'}
image_files = sorted([f for f in cwd.iterdir() if f.suffix in exts])
if not image_files:
print(f"Aucune image JPEG/PNG trouvée dans {cwd}")
sys.exit(0)
print(f"Trouvé {len(image_files)} image(s). Entrée pour voir chacune, 'q' + Entrée pour quitter.")
for img_path in image_files:
try:
img = Image.open(img_path)
print(f"\n→ {img_path.name} ({img.width}×{img.height}, {img.mode})")
img.show() # Ouvre dans visualiseur OS défaut (Preview, Photos, Eye of GNOME, etc.)
except Exception as e:
print(f" ✗ Échec ouverture {img_path.name}: {e}")
continue
# Attente utilisateur
try:
user_in = input(" [Entrée=suivant, q=quitter] ").strip().lower()
if user_in == 'q':
break
except (EOFError, KeyboardInterrupt):
break
print("\nFini.")
if __name__ == "__main__":
main()
Comment ça marche
Path.cwd()récupère dossier courant.iterdir()+ filtre suffixe trouve images.Image.open()lit métadonnées sans charger raster complet (paresseux).img.show()sauve BMP/PNG temporaire et lance visualiseur OS — non-bloquant.- Boucle attend votre frappe avant image suivante.
Améliorations possibles
- Recherche récursive :
cwd.rglob('*'). - Redimensionner grosses images preview plus rapide :
img.thumbnail((1920,1080))avantshow(). - Rotation auto EXIF :
ImageOps.exif_transpose(img). - Version GUI avec
tkinterouPyQtpour suivant/précédent/zoom/supprimer.
Sauvegardez, chmod +x view_images.py, lancez depuis votre dossier photos. Simple, peu dépendances, marche partout où Python tourne.
