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:
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)
Note: You must open the image with Image.open() pillow method.
Pillow - Image file size
The image size syntax:
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)
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)
Width: 1395 Height: 864
Pillow - Image file Format (Type)
The image file format can be:
Here is the syntax for getting the image file 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)
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:
The image mode example:
# Open Image im = Image.open("py.jpg") # Print the image mode print(im.mode)