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.filenameNow 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.jpgNote: You must open the image with Image.open() pillow method.
Pillow - Image file size
The image size syntax:
image.sizeIn 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: 864Pillow - 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.formatHere is an example for getting the image file format:
# Open Image
im = Image.open("py.jpg")
# Print the image format
print(im.format)Output:
JPEGPillow - 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.modeThe image mode example:
# Open Image
im = Image.open("py.jpg")
# Print the image mode
print(im.mode)Output:
RGB