python – How do I get the picture size with PIL?

The Question :

310 people think this question is useful

How do I get a size of a pictures sides with PIL or any other Python library?

The Question Comments :

The Answer 1

544 people think this answer is useful
from PIL import Image

im ='whatever.png')
width, height = im.size

According to the documentation.

The Answer 2

84 people think this answer is useful

You can use Pillow (Website, Documentation, GitHub, PyPI). Pillow has the same interface as PIL, but works with Python 3.


$ pip install Pillow

If you don’t have administrator rights (sudo on Debian), you can use

$ pip install --user Pillow

Other notes regarding the installation are here.


from PIL import Image
with as img:
    width, height = img.size


This needed 3.21 seconds for 30336 images (JPGs from 31×21 to 424×428, training data from National Data Science Bowl on Kaggle)

This is probably the most important reason to use Pillow instead of something self-written. And you should use Pillow instead of PIL (python-imaging), because it works with Python 3.

Alternative #1: Numpy (deprecated)

I keep scipy.ndimage.imread as the information is still out there, but keep in mind:

imread is deprecated! imread is deprecated in SciPy 1.0.0, and [was] removed in 1.2.0.

import scipy.ndimage
height, width, channels = scipy.ndimage.imread(filepath).shape

Alternative #2: Pygame

import pygame
img = pygame.image.load(filepath)
width = img.get_width()
height = img.get_height()

The Answer 3

6 people think this answer is useful

Since scipy‘s imread is deprecated, use imageio.imread.

  1. Install – pip install imageio
  2. Use height, width, channels = imageio.imread(filepath).shape

The Answer 4

3 people think this answer is useful

This is a complete example loading image from URL, creating with PIL, printing the size and resizing…

import requests
h = { 'User-Agent': 'Neo'}
r = requests.get("", headers=h)

from PIL import Image
from io import BytesIO
# create image from binary content
i =

width, height = i.size
print(width, height)
i = i.resize((100,100))

The Answer 5

1 people think this answer is useful

Here’s how you get the image size from the given URL in Python 3:

from PIL import Image
import urllib.request
from io import BytesIO

file = BytesIO(urllib.request.urlopen('').read())
im =
width, height = im.size

The Answer 6

1 people think this answer is useful

Note that PIL will not apply the EXIF rotation information (at least up to v7.1.1; used in many jpgs). A quick fix to accomodate this:

def get_image_dims(file_path):
    from PIL import Image as pilim
    im =
    # returns (w,h) after rotation-correction
    return im.size if im._getexif().get(274,0) < 5 else im.size[::-1]

The Answer 7

0 people think this answer is useful

Followings gives dimensions as well as channels:

import numpy as np
from PIL import Image

with as img:
    shape = np.array(img).shape

Add a Comment