Get Image Filename with Pillow in Python

Pillow – Get Image Filename

To get the filename of an image with Pillow in Python, you can use PIL.Image.filename attribute.

The attribute returns the filename or path of the source file.

Examples

1. Get file name of given image

In the following program, we open an image using Image.open(), and then get the filename of the image using filename attribute of Image class object.

Python Program

from PIL import Image

# Open an image
img = Image.open("test_image_house.jpg")

# Get image file name
filename = img.filename
print(filename)
Copy

Output

test_image_house.jpg

Summary

In this Python Pillow Tutorial, we learned how to get the filename of an image using PIL.Image.filename attribute, with the help of examples.

Related Tutorials

Code copied to clipboard successfully 👍