Last modified: Jan 10, 2023 By Alexander Williams
Python-Pillow: How to get Image Filename, Size, Format, and Mode
In this article, we'll learn how to get the file name, size, format, and mode of an image.
Pillow - Image filename
To get the image file name, we need to follow the subsequent syntax:
image.filename
Now let's get the file name with an example.
from PIL import Image
# Open Image
im = Image.open("py.jpg")
# Print the file name
print(im.filename)
Output:
py.jpg
Note: You must open the image with Image.open() pillow method.
Pillow - Image file size
The image size syntax:
image.size
In the following example, we'll see how to get the image file size:
# Open Image
im = Image.open("py.jpg")
# Print the image size
print(im.size)
Output:
(1395, 864)
As you can see, the result is a tuple. The size is in pixels.
Width: 1395, Height: 864
The following program will print Width and Height:
# Print Width
print("Width:", im.width)
# Print Height
print("Height:", im.height)
Output:
Width: 1395
Height: 864
Pillow - Image file Format (Type)
The image file format can be:
- JPEG
- PNG
- GIF
- SVG
- WebP
Here is the syntax for getting the image file format:
image.format
Here is an example for getting the image file format:
# Open Image
im = Image.open("py.jpg")
# Print the image format
print(im.format)
Output:
JPEG
Pillow - Image File Mode
Mode is the numbers and names of the bands in the image. It will be “RGB” if the image color is true and CMYK if the image is pre-press.
The image mode syntax:
image.mode
The image mode example:
# Open Image
im = Image.open("py.jpg")
# Print the image mode
print(im.mode)
Output:
RGB