7

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

Boaz
  • 4,864
  • 12
  • 50
  • 90

2 Answers2

10

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.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • Thanks @Lukas - so what do you recommend (assuming I want to behave like a browser/imageviewer etc. – Boaz Oct 25 '14 at 11:38
  • Hmm, hard to say. Fortunately I never have been in a situation where I had to deal with this. I probably would check if the image has a non-conventional orientation, and if so, normalize it and [apply the rotation to the actual image data](http://stackoverflow.com/questions/1606587/how-to-use-pil-to-resize-and-apply-rotation-exif-information-to-the-file). – Lukas Graf Oct 25 '14 at 11:43
2

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)

Community
  • 1
  • 1
user3666197
  • 1
  • 6
  • 50
  • 92