Unverified Commit 4450bd2e authored by David de la Iglesia Castro's avatar David de la Iglesia Castro Committed by GitHub
Browse files

Hande exif orientation with `pillow` backend in `imread` (#805)

* Add breaking test

* Handle exif orientation tag
parent 9e601ca6
...@@ -15,7 +15,7 @@ except ImportError: ...@@ -15,7 +15,7 @@ except ImportError:
TJCS_RGB = TJPF_GRAY = TJPF_BGR = TurboJPEG = None TJCS_RGB = TJPF_GRAY = TJPF_BGR = TurboJPEG = None
try: try:
from PIL import Image from PIL import Image, ImageOps
except ImportError: except ImportError:
Image = None Image = None
...@@ -92,6 +92,8 @@ def _pillow2array(img, flag='color', channel_order='bgr'): ...@@ -92,6 +92,8 @@ def _pillow2array(img, flag='color', channel_order='bgr'):
if array.ndim >= 3 and array.shape[2] >= 3: # color image if array.ndim >= 3 and array.shape[2] >= 3: # color image
array[:, :, :3] = array[:, :, (2, 1, 0)] # RGB to BGR array[:, :, :3] = array[:, :, (2, 1, 0)] # RGB to BGR
else: else:
# Handle exif orientation tag
img = ImageOps.exif_transpose(img)
# If the image mode is not 'RGB', convert it to 'RGB' first. # If the image mode is not 'RGB', convert it to 'RGB' first.
if img.mode != 'RGB': if img.mode != 'RGB':
if img.mode != 'LA': if img.mode != 'LA':
......
...@@ -26,6 +26,7 @@ class TestIO: ...@@ -26,6 +26,7 @@ class TestIO:
cls.gray_img_dim3_path = osp.join(cls.data_dir, 'grayscale_dim3.jpg') cls.gray_img_dim3_path = osp.join(cls.data_dir, 'grayscale_dim3.jpg')
cls.gray_alpha_img_path = osp.join(cls.data_dir, 'gray_alpha.png') cls.gray_alpha_img_path = osp.join(cls.data_dir, 'gray_alpha.png')
cls.palette_img_path = osp.join(cls.data_dir, 'palette.gif') cls.palette_img_path = osp.join(cls.data_dir, 'palette.gif')
cls.exif_img_path = osp.join(cls.data_dir, 'color_exif.jpg')
cls.img = cv2.imread(cls.img_path) cls.img = cv2.imread(cls.img_path)
def assert_img_equal(self, img, ref_img, ratio_thr=0.999): def assert_img_equal(self, img, ref_img, ratio_thr=0.999):
...@@ -174,6 +175,16 @@ class TestIO: ...@@ -174,6 +175,16 @@ class TestIO:
mmcv.use_backend('cv2') mmcv.use_backend('cv2')
# consistent exif behaviour
img_cv2_exif = mmcv.imread(self.exif_img_path)
img_pil_exif = mmcv.imread(self.exif_img_path, backend='pillow')
assert img_cv2_exif.shape == img_pil_exif.shape
img_cv2_exif_unchanged = mmcv.imread(
self.exif_img_path, flag='unchanged')
img_pil_exif_unchanged = mmcv.imread(
self.exif_img_path, backend='pillow', flag='unchanged')
assert img_cv2_exif_unchanged.shape == img_pil_exif_unchanged.shape
def test_imfrombytes(self): def test_imfrombytes(self):
# backend cv2, channel order: bgr # backend cv2, channel order: bgr
mmcv.use_backend('cv2') mmcv.use_backend('cv2')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment