python - 读取块中的图像文件后图像损坏
问题描述
所以我正在尝试以块的形式准备 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
所以这是我的代码,任何帮助将不胜感激,谢谢。
解决方案
推荐阅读
- php - 问:允许 API 访问否则被 FilesMatch 拒绝的文件
- spring - Spring Boot + Mongo - com.mongodb.BasicDocument,您不能添加第二个“id”标准
- elixir - Enum.filter 除了一个元素
- scala - Flink 可以为单个表/窗口处理约 50 GB 的状态吗?
- jmeter - 如何将从 Chrome 导出的 .HAR 文件导入 JMeter?
- flutter - 如何缓存手机联系人?
- c# - 从服务器接收结果时发生传输级错误错误 C#
- sql - 为什么我在运行此代码时收到“无效标识符”oracle 错误?
- python - Python 词云 - TypeError:预期的字符串
- python - Pycharm 内联文档不适用于 Macbook 中的 python 3.7