Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are width and height of an image are inverted when loading using PIL versus cv2?

I am loading an image using PIL and OpenCV packages. The height and width are reversed when loading the image using PIL versus when loading using cv2. Following is the code to print height and width of the image loaded using both the packages.

file = 'conceptual_captions/VL-BERT/data/conceptual-captions/val_image/00002725.jpg'
# load image using PIL
import PIL.Image
pil = PIL.Image.open(file).convert('RGB')
w, h = pil.size
print("width: {}, height: {}".format(w, h))

Print output width: 1360, height: 765

# now using cv2
import cv2
im = cv2.imread(file)
print("height, width, channels: {}".format(im.shape)) 

print output height, width, channels: (1360, 765, 3)

I downloaded the image and checked the size of the image using info option on Mac. Info has width = 765 and height = 1360, which is same as reported by cv2 method. Why is PIL giving wrong image dimensions?

The problem occurs with very few images. The image I have linked is one such image. For rest of the images, the height and width reported by PIL and cv2 are the same.

like image 209
Gaurav Srivastava Avatar asked Mar 03 '23 01:03

Gaurav Srivastava


1 Answers

The image has some EXIF meta data including information about orientation (rotation). I suggest reading this Q&A and subsequent references there.

Nevertheless, the proposed solution there can be simplified nowadays, just use PIL.ImageOps.exif_transpose():

If an image has an EXIF Orientation tag, return a new image that is transposed accordingly. Otherwise, return a copy of the image.

Some code to test:

from PIL import Image, ImageOps

# Read original image, show width and height
file = '...'
pil = Image.open(file).convert('RGB')
w, h = pil.size
print("width: {}, height: {}".format(w, h))

# Transpose with respect to EXIF data
pil = ImageOps.exif_transpose(pil)
w, h = pil.size
print("width: {}, height: {}".format(w, h))

The corresponding output:

width: 1360, height: 765
width: 765, height: 1360
----------------------------------------
System information
----------------------------------------
Platform:     Windows-10-10.0.16299-SP0
Python:       3.8.5
Pillow:       7.2.0
----------------------------------------
like image 123
HansHirse Avatar answered Mar 16 '23 20:03

HansHirse