Get Image Filename from Image PIL

Viewed 35730

Is it possible to get the filename of an Image I have already opened from an Image object? I checked the API, and the best I could come up with was the PIL.Image.info, but that appears to be empty when I check it. Is there something else I can use to get this info in the PIL Image library?

(Yes, I realize I can pass the filename into the function. I am looking for another way to do this.)

i.e.

from PIL import Image

def foo_img(img_input):
  filename = img_input.info["filename"]
  # I want this to print '/path/to/some/img.img'
  print(filename) 

foo_img(Image.open('/path/to/some/img.img'))
3 Answers

Another way how I did it is by using the initial file location:

def getImageName(file_location):
    filename = file_location.split('/')[-1]
    location = file_location.split('/')[0:-1]
    filename = filename.split('.')
    filename[0] += "_resized"
    filename = '.'.join(filename)
    new_path = '/'.join(location) + '/' + filename
    return new_path
Related