使用PIL for Django网站旋转通过电子邮件上传的图像

时间:2021-08-10 00:25:21

My website allows users to upload photos to their gallery via email and it works perfectly. However, photos taken on the iPhone in portrait mode do NOT rotate correctly. I would like to rotate the photo using PIL during the "mail filtering" process. Here is the code that I am using to successfully retrieve the image from the email and save to my Django model

我的网站允许用户通过电子邮件将照片上传到他们的画廊,它完美无缺。但是,在纵向模式下在iPhone上拍摄的照片无法正确旋转。我想在“邮件过滤”过程中使用PIL旋转照片。这是我用来成功从电子邮件中检索图像并保存到我的Django模型的代码

    image = ContentFile(b64decode(part.get_payload()))
    img = Photo(user=user)
    filename = part.get_filename().lower()
    img.img.save(filename, image)
    img.save()

*Updated code that successfully rotates temp image to local dir *

*更新了成功将临时图像旋转到本地目录的代码*

     image = ContentFile(b64decode(part.get_payload()))
     im = Image.open(image)
     tempfile = im.rotate(90)
     tempfile.save("/srv/www/mysite.com/public_html/media/images/rotate.jpg", "JPEG")
     img = Photo(user=user)
     img.img.save('rotate.jpg', tempfile)
     img.save()

Now, I'm trying to take the "temp image" and save it to my model. Unfortunately, it is not saving. Any suggestions would be greatly appreciated.

现在,我正在尝试拍摄“临时图像”并将其保存到我的模型中。不幸的是,它不是在储蓄。任何建议将不胜感激。

1 个解决方案

#1


1  

http://effbot.org/imagingbook/image.htm

clearly states that rotate() returns an new image instance.

清楚地说明rotate()返回一个新的图像实例。

There is nothing in the documentation about in-place operations. Or?

文档中没有关于就地操作的任何内容。要么?

#1


1  

http://effbot.org/imagingbook/image.htm

clearly states that rotate() returns an new image instance.

清楚地说明rotate()返回一个新的图像实例。

There is nothing in the documentation about in-place operations. Or?

文档中没有关于就地操作的任何内容。要么?