Skip to main content
 首页 » 编程设计

python之如何将图像保存到磁盘

2025年05月04日18birdshome

I working on a mobile application, where user can upload images, am using Django in server side.

我有两种方式获取图片,我想在异常(exception)情况下将图片保存到磁盘。

imageFile = request.FILES['image'] 
try: 
    file_path = imageFile.temporary_file_path() 
except AttributeError as e: 
    logger.debug(virtualFile') 
    imageStringBuffer = imageFile.read() 
    virtualFile = StringIO.StringIO(imageStringBuffer) 
    # want to  save the  imageStringBuffer to disk 

I want to to save the 'virtualFile' (in except case) to the disk , How can I do it?

请您参考如下方法:

def handle_uploaded_file(f): 
    with open('imgepath', 'wb+') as destination: 
        for chunk in f.chunks(): 
            destination.write(chunk) 

引用:here