Django使用Piexif在MemoryUploadedFile中更新

2024-05-29 07:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正在使用piexif作为this answer来去除exif元数据。但是,piexif.insert() documentation需要修改图像的路径。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缓冲区引起的


Tags: the数据方法图像image服务器bytespil
1条回答
网友
1楼 · 发布于 2024-05-29 07:42:21

如果用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上的文档是here

相关问题 更多 >

    热门问题