Django管理员上传和映像到s3,然后调整图像大小并保存拇指问题 [英] Django admin upload and image to s3 and then resize the image and save a thumb problem

查看:115
本文介绍了Django管理员上传和映像到s3,然后调整图像大小并保存拇指问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错误地尝试上传和调整图像s3与pil和botos3和django default_storage之后出现错误。我试图在管理员中保存。

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.

这里是代码:

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,'没有这样的文件或目录')或其他一些错误。

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.

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

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)

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

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

推荐答案

为什么不尝试 sorl-thumbnail 。它具有与默认的ImageField django提供完全相同的界面,并且它似乎比滚动自己的支持更好。

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.


  • 存储支持

  • 可插拔引擎支持(PIL,pgmagick)

  • 可插拔键值存储支持(redis,缓存db) / li>
  • 可插拔后端支持

  • 管理员集成可删除

  • 虚拟一代

  • 灵活,简单的语法,不生成HTML

  • 用于删除缩略图的模型的ImageField

  • CSS样式裁剪选项

  • 垂直定位的保证金计算

  • 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

这篇关于Django管理员上传和映像到s3,然后调整图像大小并保存拇指问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆