我使用s3boto将Amazon S3作为我的存储后端。我有一个带有ImageField的Image模型。当通过管理员上传图像时,它成功地上传到S3。现在我正在尝试使用Pillow在保存后创建缩略图。我已经验证了缩略图是否被创建,方法是调用show()方法,但由于某种原因它没有被上传到S3。我认为我保存的方式可能是错误的 - 任何建议将不胜感激。
tasks.py
from celery import shared_task
from .models import Image
import os
from django.core.files.storage import default_storage as storage
from PIL import Image as PillowImage
@shared_task
def create_thumbnails(pk):
try:
image = Image.objects.get(pk=pk)
except Image.ObjectDoesNotExist:
pass
thumbnail_size = (450,200)
filename, ext = os.path.splitext(image.image.name)
try:
fh = storage.open(image.image.name, 'r')
im = PillowImage.open(fh)
im.thumbnail(thumbnail_size)
im.show() # TEST - This opens the resized image in Preview on my mac
filename = filename +'_thumbnail' +ext
new_file = storage.open(filename, 'w')
im.save(new_file, "PNG")
new_file.close()
except IOError as error:
print("cannot create thumbnail for ", filename, 'error ', error)
堆栈
Django 1.85
Python 2.7.10