PIL resize시 이미지 회전되는 현상

GisangLee·2022년 12월 2일
0

python

목록 보기
4/8

1. 해결 방법

from PIL import Image

def block_transpose(image):
  try:
    image_exif = image._getexif()
    image_orientation = image_exif[274]
    
    if image_orientation in (2,'2'):
        return image.transpose(Image.FLIP_LEFT_RIGHT)
    elif image_orientation in (3,'3'):
        return image.transpose(Image.ROTATE_180)
    elif image_orientation in (4,'4'):
        return image.transpose(Image.FLIP_TOP_BOTTOM)
    elif image_orientation in (5,'5'):
        return image.transpose(Image.ROTATE_90).transpose(Image.FLIP_TOP_BOTTOM)
    elif image_orientation in (6,'6'):
        return image.transpose(Image.ROTATE_270)
    elif image_orientation in (7,'7'):
        return image.transpose(Image.ROTATE_270).transpose(Image.FLIP_TOP_BOTTOM)
    elif image_orientation in (8,'8'):
        return image.transpose(Image.ROTATE_90)
    else:
        return image
        
  except (KeyError, AttributeError, TypeError, IndexError):
    
      return image

original_image = Image.open(s3_response["Body"])
fixed_image = block_transpose(original_image)
fixed_image.thumbnail(size, Image.LANCZOS)
fixed_image.save("new_01.jpg", format=original_image.format, optimize=True, quality=90)
profile
포폴 및 이력서 : https://gisanglee.github.io/web-porfolio/

0개의 댓글