Django图片上传和调整大小

时间:2011-08-11 04:06:27

标签: python django python-imaging-library image-uploading

我有一个带有图像字段的标准Django表单。上传图片时,我想确保图片不超过300像素×300像素。这是我的代码:

def post(request):
    if request.method == 'POST':
        instance = Product(posted_by=request.user)
        form = ProductModelForm(request.POST or None, request.FILES or None)
        if form.is_valid():
           new_product = form.save(commit=False)
           if 'image' in request.FILES:
              img = Image.open(form.cleaned_data['image'])
              img.thumbnail((300, 300), Image.ANTIALIAS)

              # this doesnt save the contents here...
              img.save(new_product.image)

              # ..because this prints the original width (2830px in my case)
              print new_product.image.width

我面临的问题是,我不清楚如何将Image类型转换为ImageField类型的类型。

6 个答案:

答案 0 :(得分:3)

来自ImageField save method的文档:

  

请注意,content参数应该是django.core.files.File的实例,而不是Python的内置文件对象。

这意味着您需要将PIL.Imageimg)转换为Python文件对象,然后将Python对象转换为django.core.files.File对象。像这样的东西(我没有测试过这段代码)可能会起作用:

img.thumbnail((300, 300), Image.ANTIALIAS)

# Convert PIL.Image to a string, and then to a Django file
# object. We use ContentFile instead of File because the
# former can operate on strings.
from django.core.files.base import ContentFile
djangofile = ContentFile(img.tostring())
new_product.image.save(filename, djangofile)

答案 1 :(得分:1)

答案 2 :(得分:1)

你去,只需改变一点以满足你的需要:

class PhotoField(forms.FileField, object):

    def __init__(self, *args, **kwargs):
        super(PhotoField, self).__init__(*args, **kwargs)
        self.help_text = "Images over 500kb will be resized to keep under 500kb limit, which may result in some loss of quality"

    def validate(self,image):
        if not str(image).split('.')[-1].lower() in ["jpg","jpeg","png","gif"]:
            raise ValidationError("File format not supported, please try again and upload a JPG/PNG/GIF file")

    def to_python(self, image):
        try:
            limit = 500000
            num_of_tries = 10
            img = Image.open(image.file)
            width, height = img.size
            ratio = float(width) / float(height)

            upload_dir = settings.FILE_UPLOAD_TEMP_DIR if settings.FILE_UPLOAD_TEMP_DIR else '/tmp'
            tmp_file = open(os.path.join(upload_dir, str(uuid.uuid1())), "w")
            tmp_file.write(image.file.read())
            tmp_file.close()

            while os.path.getsize(tmp_file.name) > limit:
                num_of_tries -= 1
                width = 900 if num_of_tries == 0 else width - 100
                height = int(width / ratio)
                img.thumbnail((width, height), Image.ANTIALIAS)
                img.save(tmp_file.name, img.format)
                image.file = open(tmp_file.name)
                if num_of_tries == 0:
                    break                    
        except:
            pass
        return image

来源:http://james.lin.net.nz/2012/11/19/django-snippet-reduce-image-size-during-upload/

答案 3 :(得分:1)

以下是一款可以解决这个问题的应用:django-smartfields

from django.db import models

from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor

class Product(models.Model):
    image = fields.ImageField(dependencies=[
        FileDependency(processor=ImageProcessor(
            scale={'max_width': 300, 'max_height': 300}))
    ])

答案 4 :(得分:0)

在此处尝试我的解决方案:https://stackoverflow.com/a/25222000/3731039

<强>突出显示

  • 使用Pillow进行图像处理(需要两个包:libjpeg-dev,zlib1g-dev)
  • 使用Model和ImageField作为存储
  • 使用HTTP POST或PUT与multipart / form
  • 无需手动将文件保存到磁盘。
  • 创建多个分辨率并存储其尺寸。

答案 5 :(得分:0)

您可以使用我的库django-sizedimagefield,它没有额外的依赖性,并且使用起来非常简单。