Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hande exif orientation with pillow backend in imread #805

Merged
merged 2 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mmcv/image/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
TJCS_RGB = TJPF_GRAY = TJPF_BGR = TurboJPEG = None

try:
from PIL import Image
from PIL import Image, ImageOps
except ImportError:
Image = None

Expand Down Expand Up @@ -92,6 +92,8 @@ def _pillow2array(img, flag='color', channel_order='bgr'):
if array.ndim >= 3 and array.shape[2] >= 3: # color image
array[:, :, :3] = array[:, :, (2, 1, 0)] # RGB to BGR
else:
# Handle exif orientation tag
img = ImageOps.exif_transpose(img)
# If the image mode is not 'RGB', convert it to 'RGB' first.
if img.mode != 'RGB':
if img.mode != 'LA':
Expand Down
Binary file added tests/data/color_exif.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/test_image/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def setup_class(cls):
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.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)

def assert_img_equal(self, img, ref_img, ratio_thr=0.999):
Expand Down Expand Up @@ -174,6 +175,16 @@ def test_imread(self):

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):
# backend cv2, channel order: bgr
mmcv.use_backend('cv2')
Expand Down