433

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

I159
  • 29,741
  • 31
  • 97
  • 132

7 Answers7

765
from PIL import Image

im = Image.open('whatever.png')
width, height = im.size

According to the documentation.

Gary Sheppard
  • 4,764
  • 3
  • 25
  • 35
phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • 15
    If you also want to know the number of channels, you should use ```im.mode```. Since PIL is a bit cryptic, you can also use numpy: ```numpy.array(im).shape``` – Alex Kreimer Jun 17 '17 at 17:26
  • 19
    Note @AlexKreimer that using `.shape` results in different returns since height is the first of the 2d array, then width. Therefore `height, width = np.array(im).shape` – Jack Hales May 18 '19 at 13:53
  • 3
    Please use `with`. – Shital Shah Apr 10 '20 at 05:05
  • 1
    @AlexKreimer : `np.array(im).shape` does NOT return number of channels, it rather returns `height` and `width`! – Farid Alijani May 11 '20 at 13:04
  • 1
    @FäridAlijani sure, it returns the shape of a tensor, which (possibly) includes the number of channels. If you only get 2 dims it probably means that the number of channels is 1. – Alex Kreimer May 12 '20 at 08:59
  • I am afraid it is not true though, cuz I tried this on RGB image with 3 channels! – Farid Alijani May 12 '20 at 09:02
  • 4
    It may be worth noting that `Image.open()` reads the metadata without loading the full image, which may have pros (faster) or cons (duplication) depending on your use case. Use `Image.load()` to read the full image. https://stackoverflow.com/a/19034942/1295595 – craq Jun 04 '20 at 03:16
109

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

Installation

$ 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.

Code

from PIL import Image
with Image.open(filepath) as img:
    width, height = img.size

Speed

This needed 3.21 seconds for 30336 images (JPGs from 31x21 to 424x428, 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()
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
9

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

  1. Install - pip install imageio
  2. Use height, width, channels = imageio.imread(filepath).shape
bluesummers
  • 11,365
  • 8
  • 72
  • 108
  • `imageio` is based on Pillow and provides a common API for different file formats. So performance should be similar to that of Pillow. – caram Jul 08 '20 at 06:46
4

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("https://images.freeimages.com/images/large-previews/85c/football-1442407.jpg", headers=h)

from PIL import Image
from io import BytesIO
# create image from binary content
i = Image.open(BytesIO(r.content))


width, height = i.size
print(width, height)
i = i.resize((100,100))
display(i)
prosti
  • 42,291
  • 14
  • 186
  • 151
2

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 = pilim.open(file_path)
    # returns (w,h) after rotation-correction
    return im.size if im._getexif().get(274,0) < 5 else im.size[::-1]
Oliver Zendel
  • 2,695
  • 34
  • 29
1

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('http://getwallpapers.com/wallpaper/full/b/8/d/32803.jpg').read())
im = Image.open(file)
width, height = im.size
0

Followings gives dimensions as well as channels:

import numpy as np
from PIL import Image

with Image.open(filepath) as img:
    shape = np.array(img).shape
Shital Shah
  • 63,284
  • 17
  • 238
  • 185