Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to preserve exif data with ProcessedImageField #563

Open
zoltan-ky opened this issue May 18, 2023 · 1 comment
Open

How to preserve exif data with ProcessedImageField #563

zoltan-ky opened this issue May 18, 2023 · 1 comment

Comments

@zoltan-ky
Copy link

The old options={'keep_exif': True} seems to be gone from imagekit / pilkit / PIL. Now it seems one has to supply the 'exif' data itself from the uploaded source file in options -> params to PIL Image.save() - so how does one do that as it cannot be declared statically as ProcessedImageField options parameter?

@epou
Copy link

epou commented Jul 12, 2023

Here's a workaround I ended up using for facing this issue. Not fancy, I'd prefer doing it by setting an option in the ProcessedImageField. It basically overrides the save method of the original ProcessedImageField by setting the new spec.options dict to contain the same EXIF info as the source image (before processing it)

import os

from django.db.models.fields.files import ImageFieldFile
from django.db import models

from imagekit.models import ProcessedImageField as OriginalProcessedImageField
from imagekit.utils import generate, open_image, suggest_extension


class ProcessedImageExifFieldFile(ImageFieldFile):
    def save(self, name, content, save=True):
        # COPIED FROM OriginalProcessedImageField
        filename, ext = os.path.splitext(name)
        spec = self.field.get_spec(source=content)
        ext = suggest_extension(name, spec.format)
        new_name = f"{filename}{ext}"
        # ADDED CODE START
        if not spec.options:
            spec.options = {}
        if exif := open_image(content).info.get("exif", None):
            spec.options["exif"] = exif        
        # ADDED CODE END
        content = generate(spec)
        return super().save(new_name, content, save)


class ProcessedImageField(OriginalProcessedImageField):
    attr_class = ProcessedImageExifFieldFile

####################

class DummyModel(models.Model)
    image = ProcessedImageField()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants