同一张图像在本地显示是正确的但是上传到网页或其他系统打开是旋转后的图像

原图如下:

在网页端或其他机器打开的图像如下:

出现以上现象的原因:

某些相机生成的图像编码中会自带旋转信息,所以会出现以上现象

解决方案如下:
from glob import glob
import PIL
from PIL import Image
from PIL import ExifTags
from PIL import ImageOps
def apply_exif_orientation(image):
    try:
        exif = image._getexif()
    except AttributeError:
        exif = None
    if exif is None:
        return image
    exif = {PIL.ExifTags.TAGS[k]: v for k, v in exif.items() if k in PIL.ExifTags.TAGS}
    orientation = exif.get('Orientation', None)
    print(orientation)
    if orientation == 1:
        # do nothing
        return image
    elif orientation == 2:
        # left-to-right mirror
        return PIL.ImageOps.mirror(image)
    elif orientation == 3:
        # rotate 180
        return image.transpose(PIL.Image.ROTATE_180)
    elif orientation == 4:
        # top-to-bottom mirror
        return PIL.ImageOps.flip(image)
    elif orientation == 5:
        # top-to-left mirror
        return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_270))
    elif orientation == 6:
        return image.transpose(PIL.Image.ROTATE_270)
    elif orientation == 7:
        return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_90))
    elif orientation == 8:
        return image.transpose(PIL.Image.ROTATE_90)
    else:
        return image
if __name__=="__main__":
    floder="D:/DataSet/wuzuo/v1.0/Detection_AIBEE_beijing_office_20200610_productairport/*.jpg"
    dst_path="D:/DataSet/wuzuo/v1.0/result/"
    for i in glob(floder):
        jpg_name=i.split("\\")[-1]
        image = Image.open(i)
        img=apply_exif_orientation(image)
        img.save(dst_path+jpg_name)

EXIF的Orientation一个有8个值,分别代表如下图所示:1代表原图,2代表1的镜像,3代表原图顺/逆时针旋转180°,4代表3的图像的镜像,6代表逆时针旋转90°/顺时针旋转270°,7代表6的镜像,8代表顺时针旋转90°/逆时针旋转270°,5代表8的镜像

参考文章

https://blog.csdn.net/weixin_43143224/article/details/91048404?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-3

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值