Django admin上传图像到s3然后调整图像大小并保存拇指问题

发布于 2024-10-21 04:50:39 字数 2021 浏览 2 评论 0原文

我在尝试使用 pil 和 botos3 以及 django default_storage 将图像上传到 s3 并调整其大小时遇到​​错误。我正在尝试在管理员中保存时执行此操作。

这是代码:

from django.db import models
from django.forms import CheckboxSelectMultiple

import tempfile

from django.conf import settings

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as s3_storage
from django.core.cache import cache

from datetime import datetime

import Image, os
import PIL.Image as PIL
import re, os, sys, urlparse

class screenshot(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)

    image = models.ImageField(upload_to='screenshots')
    thumbnail = models.ImageField(upload_to='screenshots-thumbs', blank=True, null=True, editable=False)

    def save(self):
        super(screenshot, self).save() # Call the "real" save() method
        if self.image:

            thumb = Image.open(self.image.path)
            thumb.thumbnail(100, 100)

            filename = str(self.slug)

            temp_image = open(os.path.join('tmp',filename), 'w')
            thumb.save(temp_image, 'JPEG')

            from django.core.files import File
            thumb_data = open(os.path.join('/tmp',filename), 'r')
            thumb_file = File(thumb_data)

            new_file.thumb.save(str(self.slug) + '.jpg', thumb_file)


    def __str__(self):
        return self.title

这只是我尝试让它工作的多种方法之一,我要么得到(2,“没有这样的文件或目录”)或其他错误。

请有人帮助我让它工作。我希望它使用 django 后端来获取上传的图像以调整大小并保存为缩略图,然后保存。如果您需要了解任何信息,请告诉我。我很乐意使用 django 片段 - http://djangosnippets.org/snippets/224/但我不知道该提供什么数据。即使主图像正常上传到 s3,我也会得到相同的 IOErrors 和“没有这样的路径/文件名”。我也尝试过这样的事情:

myimage = open(settings.MEDIA_URL + str(self.image)) 
myimage_io = StringIO.StringIO()
imageresize = myimage.resize((100,100), Image.ANTIALIAS)
imageresize.save('resize_100_100_aa.jpg', 'JPEG', quality=75)

现在已经看了三天了,所以我开始闲着了!谢谢

I am having error after error trying to upload and resize images to s3 with pil and botos3 and the django default_storage. I am trying to do this on save in the admin.

here is the code:

from django.db import models
from django.forms import CheckboxSelectMultiple

import tempfile

from django.conf import settings

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as s3_storage
from django.core.cache import cache

from datetime import datetime

import Image, os
import PIL.Image as PIL
import re, os, sys, urlparse

class screenshot(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)

    image = models.ImageField(upload_to='screenshots')
    thumbnail = models.ImageField(upload_to='screenshots-thumbs', blank=True, null=True, editable=False)

    def save(self):
        super(screenshot, self).save() # Call the "real" save() method
        if self.image:

            thumb = Image.open(self.image.path)
            thumb.thumbnail(100, 100)

            filename = str(self.slug)

            temp_image = open(os.path.join('tmp',filename), 'w')
            thumb.save(temp_image, 'JPEG')

            from django.core.files import File
            thumb_data = open(os.path.join('/tmp',filename), 'r')
            thumb_file = File(thumb_data)

            new_file.thumb.save(str(self.slug) + '.jpg', thumb_file)


    def __str__(self):
        return self.title

This is just one of the many ways I have tried to get it working, and I either get (2, 'No such file or directory') or some other error.

Please can someone help me to get it working. I want it to use the django backend to get the image uploaded to be resized and saved as the thumbnail and then saved. Let me know if you need to know any information. I would be happy to use the django snippet - http://djangosnippets.org/snippets/224/ but I don't know what data to feed it. I get the same IOErrors and 'no such path/filename' even though the main image is uploading to s3 fine. I have also tried things like:

myimage = open(settings.MEDIA_URL + str(self.image)) 
myimage_io = StringIO.StringIO()
imageresize = myimage.resize((100,100), Image.ANTIALIAS)
imageresize.save('resize_100_100_aa.jpg', 'JPEG', quality=75)

It's been 3 days of looking now so I am starting to go spare! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我遇到了类似的问题,但就我而言,使用 sorl-thumbnail 不是一个选择。我发现我可以通过传入文件描述符而不是路径来直接从 S3BotoStorage 打开图像。

而不是

thumb = Image.open(self.image.path)

使用Then。

thumb = Image.open(s3_storage.open(self.image.name))

因此,您可以像以前一样在本地处理并保存新文件,

I had a similar problem, but in my case using sorl-thumbnail was not an option. I found that I can open an Image directly from S3BotoStorage by passing in a file descriptor instead of a path.

So instead of

thumb = Image.open(self.image.path)

use

thumb = Image.open(s3_storage.open(self.image.name))

Then you can process and save the new file locally as you were doing before.

谜泪 2024-10-28 04:50:39

为什么不尝试sorl-thumbnail。它具有与 django 提供的默认 ImageField 完全相同的界面,并且看起来它比自行支持要好得多。

  • 存储支持
  • 可插拔引擎支持(PIL、pgmagick)
  • 可插拔键值存储支持(redis、缓存数据库)
  • 可插拔后端支持
  • 管理集成,可以删除
  • 虚拟生成
  • 灵活、简单的语法,不会
  • 为删除缩略图的模型生成 html ImageField
  • CSS 样式裁剪选项
  • 垂直定位的边距计算

Why don't you try sorl-thumbnail. It has the exact same interface as the default ImageField django provides and it seems like it would be a lot nicer to work with than the roll-your-own support.

  • Storage support
  • Pluggable Engine support (PIL, pgmagick)
  • Pluggable Key Value Store support (redis, cached db)
  • Pluggable Backend support
  • Admin integration with possibility to delete
  • Dummy generation
  • Flexible, simple syntax, generates no html
  • ImageField for model that deletes thumbnails
  • CSS style cropping options
  • Margin calculation for vertical positioning
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文