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

时间:2021-01-12 00:21:25

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.

错误尝试上传并使用pil和botos3以及django default_storage将图像调整为s3后出错。我想在管理员中保存这样做。

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.

这只是我试图让它工作的众多方法之一,我得到(2,'没有这样的文件或目录')或其他一些错误。

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:

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

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

现在看了3天,所以我开始有空了!谢谢

2 个解决方案

#1


5  

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.

你为什么不试试sorl-thumbnail。它具有与默认ImageField django提供的完全相同的界面,并且看起来它比使用自己的支持更好。

  • Storage support
  • 存储支持
  • Pluggable Engine support (PIL, pgmagick)
  • 可插拔引擎支持(PIL,pgmagick)
  • Pluggable Key Value Store support (redis, cached db)
  • 可插入密钥值存储支持(redis,缓存db)
  • Pluggable Backend support
  • 可插拔后端支持
  • Admin integration with possibility to delete
  • 管理员集成,可以删除
  • Dummy generation
  • 假一代
  • Flexible, simple syntax, generates no html
  • 灵活,简单的语法,不生成HTML
  • ImageField for model that deletes thumbnails
  • 用于删除缩略图的模型的ImageField
  • CSS style cropping options
  • CSS样式裁剪选项
  • Margin calculation for vertical positioning
  • 垂直定位的保证金计算

#2


7  

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.

我有类似的问题,但在我的情况下使用sorl-thumbnail不是一个选项。我发现我可以通过传入文件描述符而不是路径直接从S3BotoStorage打开一个Image。

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.

然后,您可以像以前一样在本地处理和保存新文件。

#1


5  

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.

你为什么不试试sorl-thumbnail。它具有与默认ImageField django提供的完全相同的界面,并且看起来它比使用自己的支持更好。

  • Storage support
  • 存储支持
  • Pluggable Engine support (PIL, pgmagick)
  • 可插拔引擎支持(PIL,pgmagick)
  • Pluggable Key Value Store support (redis, cached db)
  • 可插入密钥值存储支持(redis,缓存db)
  • Pluggable Backend support
  • 可插拔后端支持
  • Admin integration with possibility to delete
  • 管理员集成,可以删除
  • Dummy generation
  • 假一代
  • Flexible, simple syntax, generates no html
  • 灵活,简单的语法,不生成HTML
  • ImageField for model that deletes thumbnails
  • 用于删除缩略图的模型的ImageField
  • CSS style cropping options
  • CSS样式裁剪选项
  • Margin calculation for vertical positioning
  • 垂直定位的保证金计算

#2


7  

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.

我有类似的问题,但在我的情况下使用sorl-thumbnail不是一个选项。我发现我可以通过传入文件描述符而不是路径直接从S3BotoStorage打开一个Image。

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.

然后,您可以像以前一样在本地处理和保存新文件。