The Python Oracle

How do I get the picture size with PIL?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping

--

Chapters
00:00 Question
00:18 Accepted answer (Score 737)
00:31 Answer 2 (Score 108)
02:03 Answer 3 (Score 9)
02:26 Answer 4 (Score 5)
02:55 Thank you

--

Full question
https://stackoverflow.com/questions/6444...

Accepted answer links:
[documentation]: http://effbot.org/imagingbook/image.htm

Answer 2 links:
[Website]: http://python-pillow.org/
[Documentation]: http://pillow.readthedocs.org/
[GitHub]: https://github.com/python-pillow/Pillow
[PyPI]: https://pypi.python.org/pypi/Pillow/
[here]: http://pillow.readthedocs.org/installati...
[National Data Science Bowl]: http://www.kaggle.com/c/datasciencebowl
[scipy.ndimage.imread]: https://docs.scipy.org/doc/scipy-1.1.0/r...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pythonimaginglibrary #image

#avk47



ACCEPTED ANSWER

Score 797


from PIL import Image

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

According to the documentation.




ANSWER 2

Score 113


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()



ANSWER 3

Score 9


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

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



ANSWER 4

Score 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)