matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

How to get the mode from the image? #493

Open Myzel394 opened 4 years ago

Myzel394 commented 4 years ago

I want to find out the dominant color of the image and store it in a field. This should happen in the save method.

I'm using a little version of this method: https://stackoverflow.com/a/34964548/9878135

class Model(models.Model):
    image = models.ImageField(
            storage=OverwriteStorage(),
            upload_to=image_path + "_org/",
            default=settings.DEFAULT_IMAGE_PATH,
            blank=True,
            )

    def save(self, *args, **kwargs):
        self.create_dominant_color()
        super().save(*args, **kwargs)

    def create_dominant_color(self):
        self.dominant_color = get_dominant_color(self.image)

This is my get_dominant_color:

def get_dominant_color(image_file):
    im = Image.new(image.mode, image.size)
    # just added one line

When I try to call image.mode I get this error: 'ImageCacheFile' object has no attribute 'mode'.

Would be nice if you could add the mode to the ImageCacheFile class.