Hrvoje
Hrvoje

Reputation: 15252

Python Pillow error: No such file or directory

I have Pillow function for displaying images in Pandas dataframe on Windows machine.It works ok on testing dataset.

Pill function:

from PIL import Image

def get_thumbnail(path):
    i = Image.open(path)
    print(path)
    i.show()
    return i

Than I'm using that function to new create new Pandas column that should hold PIL image info. Image is generated based on image URL which is stored in another Pandas column:

adInfoListPD['Ad_thumb']

which looks like this:

> 0   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\03_ps4-pro-500-million-limited-edition-blau-transparent-bundle-29598325900_ps4-pro-500-million-limited-edition-blau-transparent-bundle-295983259__thumb_100x75.jpg
> 1   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\04_playstation-4-20th-anniversary-edition-ungeoeffnet-29586533000_playstation-4-20th-anniversary-edition-ungeoeffnet-295865330__thumb_100x75.jpg
> 2   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\05_playstation-4-20th-anniversary-sammleredition-ovp-29496806400_playstation-4-20th-anniversary-sammleredition-ovp-294968064__thumb_100x75.jpg
> 3   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\07_gratis-versand-alles-zusammen-xxxl-paket-29517022700_gratis-versand-alles-zusammen-xxxl-paket-295170227__thumb_100x75.jpg
> 4   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\08_groesste-ankauf-mit-sofortigem-bargeld-30099513000_groesste-ankauf-mit-sofortigem-bargeld-300995130__thumb_100x75.jpg
> 5   
> C:\Users\user\Documents\001ML\willat\Games_Konsolen\09_wir-zahlen-sofort-bargeld-30099285800_wir-zahlen-sofort-bargeld-300992858__thumb_100x75.jpg

And I'm using this line to create column which will hold pill image: adInfoListPD['image'] = adInfoListPD.Ad_thumb.map(lambda f: get_thumbnail(f)).

I get error:

FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Users\\user\\Documents\\001ML\\willat\\Games_Konsolen\\03_ps4-pro-500-million-limited-edition-blau-transparent-bundle-29598325900_ps4-pro-500-million-limited-edition-blau-transparent-bundle-295983259__thumb_100x75.jpg'

I've double checked paths and they are ok. I've also read all other posts about python path problems on windows. I think I'm passing path in proper way. As I said, everything works ok on demo data, but it doesn't work with my data.

Upvotes: 2

Views: 2620

Answers (1)

Hrvoje
Hrvoje

Reputation: 15252

Path was the problem in the end.

I used:

os.path.join(dir_base,category_folder,dir_name,file_name_thumb) 

to create proper path that would work on all platforms.

I was using that already but I didn't know os.path.join works with filenames as well. So I was adding it manually and therefore omitting "\\" in path between dir_name and file_name_thumb.

And I also added "\\\\?\\" to path in Pil function:

   def get_thumbnail(path):
    path = "\\\\?\\"+path 
    i = Image.open(path)    
    return i

To fix problems with paths longer than 255 on Windows. At the same time this is not suppose to mess up path interpretation on Linux machines. But I'm not sure about that...

Upvotes: 1

Related Questions