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.