-1

When I run this code I get this error. I don't see how this is possible given that "file" is selected by the loop. I tried with absolute paths and it's the same error. The file exists and when I delete it from my directory, it's another file it can't find. `

from PIL import Image
import os

width = 200
height = 300


for file in os.listdir("images"):
    # chargez l'image et redimensionnez-la
    image = Image.open(file)
    resized_image = image.resize((width, height))

    resized_image.save("images-recadre" + file)

`

how should i do to solve this ?

  • "./"+file should do it – Lexpj Dec 24 '22 at 19:28
  • 2
    This is because `os.listdir` just returns the file name. You need `Image.open('images/'+file)`. `"./"+file` will not do it; if it did, there would be no need for the `./`. – Tim Roberts Dec 24 '22 at 19:28
  • 2
    `Image.open(file)` This assumes file is in the current directory. But it is actually in the `images` subdirectory. – John Gordon Dec 24 '22 at 19:40
  • 2
    [pathlib](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) is another option. ```iterdir``` as opposed to ```listdir```. – jwal Dec 24 '22 at 19:42
  • @jwal Very helpful link to migrate from `os` to `pathlib`. It's safer, see [my answer](https://stackoverflow.com/a/74910347/5730279). – hc_dev Dec 24 '22 at 21:14

2 Answers2

0

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)
hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 1
    If you are aware that this is a duplicate (and even voted to close as such), why post an answer as well? If you think you have something to add that is already covered in the other answers, you can post the answer on the duplicate from 7 years ago – Tomerikoo Dec 25 '22 at 08:10
  • @Tomerikoo I flagged as duplicate because of common `os.listdir` misconception. On the other hand instead of fixing the old functional approach with `os`, teaching migration using [sane](https://peps.python.org/pep-0428/#sane-behaviour) `pathlib` seems so suitable here. – hc_dev Dec 26 '22 at 09:04
  • Which is [also covered](https://stackoverflow.com/a/73667037/6045800) in the duplicate... – Tomerikoo Dec 26 '22 at 09:06
  • @Tomerikoo I posted my [answer on another duplicate](https://stackoverflow.com/a/74919509/5730279). Is this and the removal here what you intended with your comment? – hc_dev Dec 26 '22 at 11:20
-1

The file is probably not in the folder. Go to your file explorer and find the image you have. Drag it into your folder with the code. Rename the file so it is easier to type in.

Ashi
  • 1
  • 1
  • The file is in the folder, because it was just returned by `os.listdir`, that's why OP tries to open it. Maybe you can find another answer by reading the docs for `os.listdir`. Hint: It says something about file names (not absolute paths). – hc_dev Dec 24 '22 at 20:44