首页 > 解决方案 > 读取块中的图像文件后图像损坏

问题描述

所以我正在尝试以块的形式准备 django 请求的图像文件,django 文件处理程序块方法对我来说效果不佳,所以我创建了一个自定义,它可以工作,但最终产品不是我所期望的,所以在阅读文件后分块并将它们以某种方式组合在一起,图像会损坏,我没有任何解决方案。

def process_download_with_progress(self, image_file, length):

    process_recoder = ProgressRecorder(self)
    print('Upload: Task Started')
    fs = FileSystemStorage()
    buffer = io.BytesIO()
    chunk_size = 0
    
    for chunk in read_chunk(image_file.file, length):  
        chunk_size += 1      
        buffer.write(chunk)
        process_recoder.set_progress(chunk_size, length, description=f'uploaded {chunk_size*length} bytes of the file')
    buffer.seek(0)
    image = ImageFile(buffer, name=image_file.name)
    fs.save(image_file.name, content=image)
    return 'Done'

def read_chunk(file_object, chunk_size=125):
    while True:
        file =  file_object.read(chunk_size)
        if not file:
            break
        yield file

所以这是我的代码,任何帮助将不胜感激,谢谢。

标签: pythondjangoimage

解决方案


推荐阅读