暂无分类
暂无标签
发布于2020-06-18 23:02 阅读(638) 评论(0) 点赞(15) 收藏(3)
0
1
2
3
4
5
6
7
8
9
原图如下:
在网页端或其他机器打开的图像如下:
出现以上现象的原因:
某些相机生成的图像编码中会自带旋转信息,所以会出现以上现象
解决方案如下:
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)
参考文章
原文链接:https://blog.csdn.net/jiao_mrswang/article/details/106794493
0
1
2
3
4
5
6
作者:9384vfnv
链接: https://www.pythonheidong.com/blog/article/424077/d2e998f6172bd058c245/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系z452as@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!