开发者

Django admin upload and image to s3 and then resize the image and save a thumb problem

开发者 https://www.devze.com 2023-02-16 06:34 出处:网络
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.

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 = op开发者_Python百科en(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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消