python 远程图片保存到本地

# 远程图片保存到本地
def save_image(image_url):
    temp_dir = 'static/temp'
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    image_name = image_url.split('/')[-1]
    file_path = os.path.join(temp_dir, image_name)
    try:
        response = requests.get(image_url)
        if response.status_code == 200:
            with open(file_path, 'wb') as f:
                f.write(response.content)
            res = {
                "msg": "保存成功",
                "success": True,
                "data":{"src": file_path}
            }
            return jsonify(res)
        else:
            res = {
                "msg": "保存失败,请检查图片路径是否正确!",
                "success": False
            }
            return jsonify(res)
    except requests.RequestException as e:
        res = {
            "msg": "保存失败,请检查图片路径是否正确",
            "success": False
        }
        return jsonify(res)