Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of images

On a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image:
http://storage.googleapis.com/cookila-533ebf752b9d1f7c1e8b4db3/IMG_0004.JPG

The code:

from PIL import Image
import urllib, cStringIO

file = cStringIO.StringIO(urllib.urlopen('http://storage.googleapis.com/cookila-533ebf752b9d1f7c1e8b4db3/IMG_0004.JPG').read())
im=Image.open(file)
print im.size

The result is - (2592, 1936)
should be the other way around

like image 830
Boaz Avatar asked Oct 25 '14 11:10

Boaz


People also ask

What does PIL image return?

The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images. Image. convert() Returns a converted copy of this image.

How does PIL resize work?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.

What is PIL image format?

Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.


2 Answers

The reason for this is that this image has Exif Orientation metadata associated with it that will cause applications that respect that property to rotate it:

# identify -verbose IMG_0004.JPG | grep Orientation
  Orientation: RightTop
    exif:Orientation: 6

Compare with a regular image:

# identify -verbose iceland_pano.jpg | grep Orientation
  Orientation: TopLeft
    exif:Orientation: 1

So the image dimensions are actually landscape (more wide than high), but it will get rotated on display by browsers, image viewers etc.

like image 61
Lukas Graf Avatar answered Sep 22 '22 12:09

Lukas Graf


A: This is a standard state.

Numpy arrays carry images ( from OpenCV2 ) with another convention once inspected by data.shape, so does the PIL/Pillow Image.size


May review and validate as in >>> Python Pillow v2.6.0 paletted PNG (256) How to add an Alpha channel?

print data.shape gives (1624, 3856) and print im.size gives (3856, 1624)

like image 44
user3666197 Avatar answered Sep 18 '22 12:09

user3666197