django中图片的保存
首先要建立图片存储的文件
from 项目名称.setting import MEDIA_ROOT
filename = request.FILES.get('前端传入参数', ‘’)
suffix = Uploadfiles.name.split(".")[-1]
part_path = part_path.strip("/") + "/"
# 生成一个图片名称
name = uuid.uuid1()
# 存入midia中的图片路径
picture_file_path = os.path.join(setting.MEDIA_ROOT, part_path + "{0}.{1}".format(name, suffix))
# 或写成 picture_file_path = os.path.join(MEDIA_ROOT, part_path + "{0}.{1}".format(name, suffix))
w, h = pic.size
#对图片进行压缩
pic.thumbnail((int(w / 1.1), int(h / 1.1)))
#保存图片到指定目录中
pic.save(picture_file_path)index_media = picture_file_path.find('/media')
# 返回给前端media中图片的路径,方便前端读取
picture_file_path = picture_file_path[index_media:]