1

我正在尝试使用 chunks 方法保存图像来处理用户尝试上传大图像的情况:

destination = open(incident.Image.path, 'wb+')
for chunk in request.FILES['image'].chunks():
    destination.write(chunk)
destination.close()

我的问题是如果不先在 Image 字段中保存一些内容,我就无法获取文件路径,如下所示:

fileName = str(int(time.time() * 1000))
imageName = fileName + '.jpg'
incident.Image.save(imageName, request.FILES['image'])

我的问题是如何在不先保存图像的情况下获得与 event.Image.path 等效的内容?我不想对路径进行硬编码,而我被卡住的原因是因为如果不获取 ImageField 声明的 upload_to 部分,我就无法获得完整的文件路径

编辑:

好的,我已经走得更远了,但我又被卡住了:

imgBuffer = StringIO.StringIO()
for chunk in request.FILES['image'].chunks():
    imgBuffer.write(chunk)

# rotate it
rotate = request.POST['rotation']
im = Image.open(imgBuffer)
im = im.rotate(float(rotate))

incident.Image.save(imageName, ContentFile(imgBuffer))

我收到 IOError 无法识别在 im = Image.open(imgBuffer) 处抛出的图像文件,有什么想法吗?

4

3 回答 3

0

图像(作为文件)保存在模型字段的pre_save方法中。这是此类自定义模型字段的工作示例(从我的项目中获得) - FixedImageField

您可能会看到我如何在函数pre_save中检索图像路径。

就我而言,我有一个复杂的方案(我必须继承图像字段),但您可以根据您的情况对其进行简化。

您可以实现自己的 _save_fixed_resolution_image(chunks,path) - 在我的情况下,它将图像转换为最大 400x400 大小的图像:

from PIL import Image
import StringIO
from yourproject.settings import MEDIA_ROOT
import os
from django.db import models

class ChunksImageField( models.ImageField ):    
    def pre_save( self, model_instance, add ):
        file = super( models.FileField, self ).pre_save(model_instance, add)
        if file and not file._committed:
            if callable( self.upload_to ):
                path = self.upload_to( model_instance, "" )
            else:
                path = self.upload_to
            file.name = path
            full_path = os.path.join( MEDIA_ROOT, path )
            chunks = _get_chunks( file.chunks() )
            self._save_image_func( model_instance, chunks, full_path )

        return file

class FixedImageField( ChunksImageField ):
    def __init__( self, *args, **kwargs ):
        super( FixedImageField, self ).__init__( *args, **kwargs )

    def _save_image_func( self, model_instance, chunks, path ):
        _save_fixed_resolution_image( chunks, path )

def _save_fixed_resolution_image( chunks, out_file_path ):
    image = _get_image( chunks )

    if image.size[ 0 ] > _image_max_size or image.size[ 1 ] > _image_max_size:
        image.thumbnail( ( _image_max_size, _image_max_size, ) )

    save_image( image, out_file_path )

def save_image( image, out_file_path ):
    image.save( out_file_path, "JPEG", quality = 100 )

def _get_chunks( chunks ):
    chunks_ = ""
    for chunk in chunks:
        chunks_ += chunk
    return chunks_

如何在您的模型中使用它:

class YourModel( models.Model ):
    image = FixedImageField( upload_to = image_path ) # substitute your image path here
于 2012-06-09T16:33:14.763 回答
0

看看 stringIO 也看看这个网站

于 2012-06-08T14:39:44.087 回答
0

我解决了主要问题如下:

myimage = MyImageModel()
upload_to = myimage.myimagefield.field.upload_to
filepath = os.path.join(settings.MEDIA_ROOT, upload_to)

没有上传两次的次要问题我做了如下:

filename = os.path.basename(url)
myimage.myimagefield = os.path.join(upload_to, filename)
myimage.save()
于 2018-09-23T16:51:10.053 回答