Amazon S3 上的枕头大小调整图像(缩略图)-Django

Pillow Resizing images (thumbnail) on Amazon S3 - Django

我正在尝试在保存图像时调整图像大小。 具体来说,我的图像存储在 Amazon S3 上,然后我使用 django-storagesboto3 第三方应用程序

保存图像时,它存储在我的 Amazon S3 存储桶中,具有如下访问权限 url:

https://s3-sa-east-1.amazonaws.com/ihost-project/media/studyoffer_images/algoritmos-para-ensenanza/15061122523583.jpg

保存和调整图像大小的代码是这样的:

class UploadStudyOffer(models.Model):
    study_offer = models.ForeignKey(StudiesOffert, related_name='uploadsstudyoffer')
    image = models.ImageField(upload_to=get_image_path)
    # images folder per object

    def save(self, *args, **kwargs):
        super(UploadStudyOffer, self).save(*args, **kwargs)
        # We first check to make sure an image exists
        if self.image:
            # Open image and check their size
            image = Image.open(self.image)
            i_width, i_height = image.size
            max_size = (100,100)

            # We resize the image if it's too large
            if i_width > 1000:
                image.thumbnail(max_size, Image.ANTIALIAS)
                image.save(self.image.path)

当我上传图片时,我收到这条消息:

Exception Type: NotImplementedError at /host/study-offer/algoritmos-para-ensenanza/edit/images/
Exception Value: This backend doesn't support absolute paths.

而且我不确定错误是在 storagesboto 后端还是在 Pillow 中进行管理。 然后在Pillow级别我在保存图片的瞬间发现了如下选项,比如:

我改段代码:

image.save(self.image.path)

至:

image.save(self.image.name)

我得到这个错误:

File "/home/bgarcial/workspace/hostayni_platform/hosts/models.py" in save
  542.                 image.save(self.image.name) #[Errno 2] No such file or directory: 'studyoffer_images/ingenieria-de-sistemas/15061122523583.jpg'

File "/home/bgarcial/.virtualenvs/hostayni/lib/python3.6/site-packages/PIL/Image.py" in save
  1725.             fp = builtins.open(filename, "w+b")

Exception Type: FileNotFoundError at /host/study-offer/algoritmos-para-ensenanza/edit/images/
Exception Value: [Errno 2] No such file or directory: 'studyoffer_images/algoritmos-para-ensenanza/1900-X-1080-Wallpapers-022.jpg'

当然,我的图片是存储在Amazon S3上的,而不是在我的本地项目或硬盘中,那么我就这样使用url参数:

我改

image.save(self.image.name)

image.save(self.image.url)

我得到这个错误:

Exception Type: FileNotFoundError at /host/study-offer/algoritmos-para-ensenanza/edit/images/
Exception Value: [Errno 2] No such file or directory: 'https://s3-sa-east-1.amazonaws.com/ihost-project/media/studyoffer_images/algoritmos-para-ensenanza/15061122523583.jpg'

获取 amazon s3 图像 URL 不起作用,即使 url 是有效的 url https://s3-sa-east-1.amazonaws.com/ihost-project/media/studyoffer_images/algoritmos-para-ensenanza/15061122523583.jpg

那我换

image.save(self.image.url)

至:

image.save(self.image.file)

我的图片上传没有任何错误,但没有调整大小,而是按原始格式上传。

我如何处理从我的应用程序上传的图像,并且它们的结果在使用后保存在 Amazon S3 上?

您可以使用 easy_thumbnails 应用程序来简化它。

如果你想在保存时裁剪图片,你可以这样做:

from easy_thumbnails.fields import ThumbnailerImageField

CROP_SETTINGS = {'size': (1000, 500), 'crop': 'smart'}

class UploadStudyOffer(models.Model):

    image =ThumbnailerImageField(upload_to=get_image_path,
                                         resize_source=CROP_SETTINGS)

或者您可以在模板中手动指定图片的大小:

{% load thumbnail %}

<img src="{% thumbnail offer.image 1000x500 crop %}" alt="" />

对 Django/Python 相当陌生,但这就是我解决问题的方法。将大文件保存到 AWS,用 Pillow 打开它,然后调整大小并保存在内存中。然后按照 django-storages help docs 的建议使用 default_storage 推送到 AWS。请注意,img.thumbnail 将仅将图像的较长边缘设置为 1000 像素来保留纵横比。 image 是 Django 模型 ImageField

from django.core.files.storage import default_storage
from io import BytesIO

..

def save(self, *args, **kwargs):
    #run save of parent class above to save original image to disk
    super().save(*args, **kwargs)

    memfile = BytesIO()

    img = Image.open(self.image)
    if img.height > 1000 or img.width > 1000:
        output_size = (1000, 1000)
        img.thumbnail(output_size, Image.ANTIALIAS)
        img.save(memfile, 'JPEG', quality=95)
        default_storage.save(self.image.name, memfile)
        memfile.close()
        img.close()