VMware ESXi and vSphere Cluster Management

Image Attributes in Pillow

Learn how to inspect Pillow Image objects for dimensions, file format, pixel mode, metadata, channels, and other useful properties.

What a Pillow Image object contains

Pillow is a Python library for opening, inspecting, transforming, and saving images. When you open or create an image, Pillow represents it with an Image object. This object contains pixel data and properties that describe the image.

An instance attribute is a value available directly from an object with dot notation. For example, img.size reads the dimensions stored on the img object.

Attributes describe an image; they do not edit it. Reading img.mode only inspects the pixel representation. Operations such as resize(), crop(), and convert() create or modify image data.

Open an image before inspecting it

The source file must be available at the path you provide. First import Image from Pillow and open the file.

from PIL import Image

with Image.open("example.jpg") as img:
    print("Size:", img.size)
    print("Format:", img.format)
    print("Mode:", img.mode)

The with statement closes the file resource after the inspection finishes. Replace example.jpg with the path to your image. Install Pillow first if necessary:

pip install Pillow

Inspect the size attribute

size is a two-value Python tuple. A tuple is an ordered, immutable collection. For an image, the first value is the width in pixels and the second value is the height in pixels.

print(img.size)

For example, output such as (1200, 1599) means the image is 1,200 pixels wide and 1,599 pixels high. This is pixel dimension information, not the file's disk size in bytes, kilobytes, or megabytes.

You can access each tuple item by index:

width = img.size[0]
height = img.size[1]

print("Width:", width)
print("Height:", height)

Tuple unpacking is often clearer:

width, height = img.size
print(width)
print(height)

The width and height attributes are also available directly:

print(img.width)
print(img.height)

Inspect the source format with format

format identifies the image format detected when Pillow opened the source file. Common values include JPEG, PNG, GIF, and TIFF.

print("Format:", img.format)

The format is not simply the filename extension. Pillow examines the file contents, so an extension and detected format can disagree. Also, format can be None for a newly created image or an image produced by an operation such as copying, cropping, or converting.

Therefore, treat format as source-file information for an opened image. Do not rely on it as permanent information about every in-memory image.

Inspect pixel representation with mode

mode describes how Pillow represents each pixel, including its channels and sometimes its transparency information. The most familiar mode is RGB, which stores red, green, and blue values for each pixel.

print("Mode:", img.mode)

Common modes include:

ModeChannel or pixel representationTypical use
1One-bit black-and-white pixelsBilevel images and masks
LOne channel of grayscale intensityBlack-and-white or grayscale images
PPalette-based pixels that refer to a color tableIndexed-color images such as some GIF files
RGBThree channels: red, green, and blueStandard color images
RGBARGB plus an alpha channelColor images that support per-pixel transparency
CMYKCyan, magenta, yellow, and black channelsSome print-oriented images

Mode affects later operations. An RGB image has three color channels but no alpha channel. An RGBA image has an additional alpha channel for transparency. An L image stores grayscale values rather than separate red, green, and blue values.

Inspect the mode before an operation that requires a particular representation. If necessary, convert explicitly:

rgb_image = img.convert("RGB")
rgba_image = img.convert("RGBA")

Print and interpret the main attributes

This complete example labels the three most commonly inspected properties:

from PIL import Image

with Image.open("example.jpg") as img:
    print("Size:", img.size)
    print("Format:", img.format)
    print("Mode:", img.mode)

Sample output might look like this:

Size: (1200, 1599)
Format: JPEG
Mode: RGB

Interpret the output as follows:

  • (1200, 1599) means 1,200 pixels wide and 1,599 pixels high.
  • JPEG is the detected source image format.
  • RGB means each pixel uses red, green, and blue channels.

Core Pillow Image attributes

AttributeTypical valueMeaningNotes
size(1200, 1599)Width and height in pixelsThe first tuple item is width; the second is height.
formatJPEG or PNGDetected source-file formatMay be None for generated or transformed images.
modeRGB or RGBAPixel-data representationIndicates channels, grayscale, palette, or transparency support.
width1200Horizontal pixel countEquivalent to img.size[0].
height1599Vertical pixel countEquivalent to img.size[1].
info{...}Dictionary of optional metadataAvailable keys depend on the file format and embedded data.

Additional attributes and metadata

Pillow Image objects expose more information than size, format, and mode. The info attribute is a dictionary containing metadata read from the file, such as textual fields, color-profile information, or format-specific details.

print(img.info)

Metadata is optional. Different files can have different keys, and some files contain no useful metadata. Do not assume that EXIF data, camera details, or a particular field exists.

Other useful properties and methods include width, height, filename, palette, and is_animated where applicable. To inspect channel-band names, use getbands():

print(img.info)
print(img.getbands())

For example, an RGB image commonly reports ('R', 'G', 'B'), while an RGBA image commonly reports ('R', 'G', 'B', 'A'). The complete attribute reference can vary by Pillow version, so consult the Pillow API documentation when you need a property not covered here.

Attribute lifecycle and limitations

Some attributes come from the loaded source file and are not guaranteed to survive later processing. A copied, cropped, converted, or newly created image may have the same or different dimensions and mode, while its original source format may no longer be meaningful.

from PIL import Image

created = Image.new("RGB", (300, 200), "white")
print(created.size)    # (300, 200)
print(created.mode)    # RGB
print(created.format)  # None

The generated image has dimensions and a mode because those properties were specified when it was created. It has no detected source-file format because it did not come from a file. When saving such an image, choose the output format explicitly through the filename or save options.

Metadata availability also depends on the input file and on how the image is processed and exported. Code should handle missing metadata and should not assume that every Image object retains all properties of its source.

Troubleshooting attribute inspection

File cannot be opened

Check for an incorrect path, a missing source file, or an unsupported or damaged image. Verify that the file exists at the supplied path and that Pillow can identify its contents.

format is None

This commonly occurs when the image was created in memory or produced by an operation such as crop(), copy(), or convert(). Treat format as information about an opened source file, and select a format explicitly when saving a generated image.

The mode is unexpected

The image may be grayscale, palette-based, CMYK, bilevel, or an image with alpha transparency. Inspect img.mode before processing and use convert() only when a particular target mode is required.

Metadata is empty or missing fields

The file may not contain those fields, its format may not support them, or prior processing may have removed them. Treat metadata keys as optional.

Dimensions are confused with file size

img.size reports pixel dimensions only. It does not report how much storage the file uses.

Practice checklist

  1. Open an image with Image.open() using a valid source path.
  2. Print img.size, then unpack it into width and height.
  3. Print img.format and compare it with the filename extension.
  4. Print img.mode and determine whether the image has grayscale, RGB, or alpha data.
  5. Inspect img.info without assuming particular metadata keys exist.
  6. Create or convert an image and check which attributes, especially format, remain meaningful.

Continue with Image Attributes in Pillow as a reference while practicing image inspection.