2

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.

Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
  • 1
    I think this one can help you, https://stackoverflow.com/a/2953843/4365143 – taipei Mar 15 '19 at 09:01
  • I actually used os.path.join(mydir, myfile) to generate path, store that in pandas column and pass than to pillow function. That's best possible way of doing it I think. – Hrvoje Mar 15 '19 at 09:52
  • 1
    Another idea to triple check (if not already done): copy the filename, dump it into [Win+R] and hit enter to see if it opens the image. Also, I'd create another test image with a shorter name in the same folder and see if that works. It hasn't reached 256 characters yet, but you never know... – Jeronimo Mar 15 '19 at 12:18
  • Tried that already, paths are ok. In padas they are displayed properly. I'm suspecting 255 limit could be problem and I tried already some of the solutions aiming at that. Customer insist naming should be as it is. I'll try test with shorter naming. – Hrvoje Mar 15 '19 at 17:28

1 Answers1

1

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...

Hrvoje
  • 13,566
  • 7
  • 90
  • 104