在保存Django ImageField之前,该如何获取内容? [英] How to get content of Django ImageField before it is saved?

查看:254
本文介绍了在保存Django ImageField之前,该如何获取内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在保存我的模型实例时调整图像的大小。

  class Picture(models.Model):
image_file = models.ImageField(upload_to =pictures)
thumb_file = models.ImageField(upload_to =pictures,editable = False)
def save(self,force_insert = False,force_update = False):
image_object = Image.open(self.image_file。路径)
#[...]没有
超级(图片,自我).save(force_insert,force_update)

问题是在保存模型之前,self.image_file.path不存在。它返回一个正确的路径,但图像还没有。因为没有图像,我无法打开它在PIL调整大小。



我想将缩略图存储在thumb_file(另一个ImageField)中,所以我需要做在保存模型之前进行处理。



是否有一个很好的打开文件的方法(可能在内存中获取tmp图像对象),或者我必须先保存整个模型,请调整大小然后再次保存?

解决方案

我使用此片段

  import Image 

def fit(file_path,max_width = None,max_height = None,save_as = None):
#打开文件
img = Image.open(file_path)

#存储原始图像的宽度和高度
w,h = img.size

#以最大值替换宽度和高度
w = int(max_width或w)
h = int(max_height or h)

#正式调整大小
img.thumbnail ((w,h),Image.ANTIALIAS)

#保存(可选)'save_as'或原始路径
img.save(save_as或file_path)

返回True

在模型中:



$ $ $ $ $ $ $ $ $ $ $ $ $ if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if self.image:
fit(self.image_file.path,settings.MAX_WIDTH,settings.MAX_HEIGHT)


I'm trying to resize an image while saving an instance of my model.

class Picture(models.Model):
  image_file = models.ImageField(upload_to="pictures")
  thumb_file = models.ImageField(upload_to="pictures", editable=False)
  def save(self, force_insert=False, force_update=False):
    image_object = Image.open(self.image_file.path)
    #[...] nothing yet
    super(Picture, self).save(force_insert, force_update)

The problem is that self.image_file.path does not exist before the model is being saved. It returns a correct path, but the image is not there yet. Since there is no image, I can't open it in PIL for resize.

I want to store the thumbnail in thumb_file (another ImageField), so I need do the processing before saving the model.

Is there a good way to open the file (maybe get the tmp image object in memory) or do I have to save the whole model first, resize and then save again ?

解决方案

I used this snippet:

import Image

def fit(file_path, max_width=None, max_height=None, save_as=None):
    # Open file
    img = Image.open(file_path)

    # Store original image width and height
    w, h = img.size

    # Replace width and height by the maximum values
    w = int(max_width or w)
    h = int(max_height or h)

    # Proportinally resize
    img.thumbnail((w, h), Image.ANTIALIAS)

    # Save in (optional) 'save_as' or in the original path
    img.save(save_as or file_path)

    return True

And in models:

def save(self, *args, **kwargs):
    super(Picture, self).save(*args, **kwargs)
    if self.image:
        fit(self.image_file.path, settings.MAX_WIDTH, settings.MAX_HEIGHT)

这篇关于在保存Django ImageField之前,该如何获取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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