bga*_*ial 3 python django amazon-s3 pillow
保存后,我尝试调整图像大小。具体来说,我的图像存储在 Amazon S3 上,然后我使用django-storages和boto3第三方应用程序
保存图像后,它会存储在我的 Amazon S3 存储桶中,其访问 url 如下所示:
保存和调整图像大小的代码是这样的:
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)
Run Code Online (Sandbox Code Playgroud)
当我上传图片时,我收到以下消息:
Exception Type: NotImplementedError at /host/study-offer/algoritmos-para-ensenanza/edit/images/
Exception Value: This backend doesn't support absolute paths.
Run Code Online (Sandbox Code Playgroud)
而且我不知道,如果错误是在管理storages或boto后端或Pillow。然后在级别Pillow我在保存图像的那一刻发现了以下选项,例如:
我更改了部分代码:
image.save(self.image.path)
Run Code Online (Sandbox Code Playgroud)
到:
image.save(self.image.name)
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
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'
Run Code Online (Sandbox Code Playgroud)
当然,我的镜像是存储在 Amazon S3 上的,而不是本地存储在我的项目或硬盘中,那么我使用url这种方式的参数:
我改变
image.save(self.image.name)
Run Code Online (Sandbox Code Playgroud)
到
image.save(self.image.url)
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
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'
Run Code Online (Sandbox Code Playgroud)
获取亚马逊 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)
Run Code Online (Sandbox Code Playgroud)
到:
image.save(self.image.file)
Run Code Online (Sandbox Code Playgroud)
我的图片上传时没有任何错误,但没有调整大小并以原始格式上传。
如何处理从我的应用程序上传的图像并将其结果保存在 Amazon S3 上以供使用后使用?
小智 5
对 Django/Python 相当陌生,但这就是我解决它的方法。将大文件保存到 AWS,使用 Pillow 打开它,然后调整大小并保存在内存中。然后default_storage按照django-storages 帮助文档的建议推送到 AWS 。请注意,img.thumbnail将仅保留图像的较长边缘设置为 1000 像素的方面。image是 Django 模型ImageField。
from django.core.files.storage import default_storage
from io import BytesIO
Run Code Online (Sandbox Code Playgroud)
..
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |