Python PIL 保存方法旋转图像

问题描述 投票:0回答:2

我在使用 PIL 保存图像时遇到了一些问题。在我的 Django 项目中,我有以下 save 方法:

from PIL import Image

class Photo:
    image = models.ImageField(verbose_name='Photos', upload_to='media/date/photos/', help_text='Photo')

    def save(self, *args, **kwargs):
        super(Photo, self).save(*args, **kwargs)
        image = Image.open(self.image)
        image.save(self.image.path)

所以,这里我用PIL打开图像并将其保存在默认路径中。但看起来 PIL 会根据图像的 EXIF 数据(元数据)在保存图像之前旋转图像。

例如,我拍了一张人的照片(正常情况下我是垂直拿着手机的),当我保存它时,照片向左旋转了90度。这里可能出了什么问题?仅当方向为垂直时才会发生这种情况,水平拍摄的图像不会发生任何不良情况。请帮助并提前非常感谢!

Original image, Saved image

python django python-imaging-library image-rotation piexif
2个回答
0
投票

回复我自己的帖子。伙计们,我刚刚想出了使用 OpenCV 而不是 PIL 的想法。它对我来说效果很好,没有发生图像旋转,并且所有内容都正确上传。希望这可以帮助你。祝你好运!


0
投票

我也有类似的问题。

将原始文件的 exif 数据添加到保存的副本中即可解决。

image.save(self.image.path, exif=self.image.info.get("exif"))

© www.soinside.com 2019 - 2024. All rights reserved.