Python Django使用Piexif在MemoryUploadedFile中更新

Python Django使用Piexif在MemoryUploadedFile中更新,python,django,python-imaging-library,piexif,Python,Django,Python Imaging Library,Piexif,我试图在保存到服务器之前,先删除上传图像上的数据,然后再继续对其进行其他处理 我使用piexif as in来去除exif元数据。但是,需要修改图像的路径。piexif可以用于写入内存中的文件吗 还记录了PIL以去除exif数据。因为我无论如何都在使用PIL,所以我宁愿使用纯PIL方法 def modifyAndSaveImage(): # Get the uploaded image as an InMemoryUploadedFile i = form.cleaned_dat

我试图在保存到服务器之前,先删除上传图像上的数据,然后再继续对其进行其他处理

我使用piexif as in来去除exif元数据。但是,需要修改图像的路径。piexif可以用于写入内存中的文件吗

还记录了PIL以去除exif数据。因为我无论如何都在使用PIL,所以我宁愿使用纯PIL方法

def modifyAndSaveImage():
    # Get the uploaded image as an InMemoryUploadedFile
    i = form.cleaned_data['image']

    # Use piexif to remove exif in-memory?
    #exif_bytes = piexif.dump({})
    #piexif.insert(exif_bytes, i._name)  # What should the second parameter be?

    # continue using i...
    im = Image.open(i)
    buffer = BytesIO()
    im.save(fp=buffer, format='JPEG', quality=95)

    return ContentFile(buffer.getvalue())

PIL的保存方法似乎是将exif数据应用于图像,而不是不使用exif保存(将旋转应用于原始图像)。或者这是由BytesIO缓冲区引起的?

如果使用
PIL加载文件并保存,它将剥离EXIF

from PIL import Image

image = Image.open(form.cleaned_data['image'])

image.save('my_images/image.jpg')
如果你有任何数据仍然存在的问题,你也可以尝试创建一个完整的新形象,像这样

from PIL import Image

image = Image.open(form.cleaned_data['image'])

image_data = list(image.getdata())

new_image = Image.new(image.mode, image.size)
new_image.putdata(image_data)
new_image.save('my_images/image.jpg')

Image
上的文档是

我实际上是在将其传递到PIL并在以下步骤中执行Image.save。剥离exif时,旋转似乎应用于图像。我认为这与我对BytesIO和ContentBuffer的使用有关。问题已用此代码更新。