Debugging
Either research, because the FileNotFoundError
in combination with os.listdir
is known and answered:
Or debug using print-statements and reading the docs about os.listdir
helps.
from PIL import Image
import os
width = 200
height = 300
for file in os.listdir("images"):
print("file name:", file) # is the printed an absolute path
# chargez l'image et redimensionnez-la
image = Image.open(file) # does this line throw the error?
resized_image = image.resize((width, height))
# does save need an absolute path?
print("save to path:", "images-recadre" + file)
resized_image.save("images-recadre" + file) # needs a path-separator
Safer to use pathlib
As jwal commented, use the object-oriented counterparts of pathlib
:
from pathlib import Path
images_folder = Path('images')
for child in images_folder.iterdir():
print("file name:", child) # is the printed an absolute path
file = child.absolute()
# chargez l'image et redimensionnez-la
image = Image.open(file) # does this line throw the error?
resized_image = image.resize((width, height))
to_path = images_folder.joinpath("images-recadre", child.name) # join adds the path-separators
print("saving to path:", to_path)
resized_image.save(to_path)