加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 将通过PIL创建的图像保存到django模型

发布时间:2020-12-16 23:40:40 所属栏目:Python 来源:网络整理
导读:我已经使用以下代码成功创建并旋转了通过电子邮件上传到我的服务器上的目录的图像: image = ContentFile(b64decode(part.get_payload())) im = Image.open(image) tempfile = im.rotate(90) tempfile.save("/srv/www/mysite.com/public_html/media/images/r
我已经使用以下代码成功创建并旋转了通过电子邮件上传到我的服务器上的目录的图像:
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()

旋转的图像存在于目录中,但是当我尝试将该图像添加到我的模型中时,它不会保存.我失踪了什么任何帮助将不胜感激.

解决方法

我用以下代码解决了这个问题:
image = ContentFile(b64decode(part.get_payload()))
       im = Image.open(image)
       tempfile = im.rotate(270)
       tempfile_io =StringIO.StringIO()
       tempfile.save(tempfile_io,format='JPEG')
       image_file = InMemoryUploadedFile(tempfile_io,None,'rotate.jpg','image/jpeg',tempfile_io.len,None)
       img = Photo(user=user)
       img.img.save('rotate.jpg',image_file)
       img.save()

我在这里找到答案How do you convert a PIL `Image` to a Django `File`?.工作无瑕疵!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读