Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Imagekit Background Fill

I'm just getting started with using Django and looking for a solution to crop logos and automatically detect the logo background to fill it. Most logos are rectangular but I actually need to have square images.

Here is an example of what I'd like to do. Note, the white background is automatically detected from the first pixel of the original image. I've been looking at Django Imagekit but still not quite clear on how to accomplish this. http://django-imagekit.readthedocs.org/en/1.1.0/#

Does anyone know a straightforward way to do this?

enter image description here

like image 975
Alex Phelps Avatar asked Jul 01 '26 02:07

Alex Phelps


1 Answers

Here's a custom processor for Django Imagekit I wrote to accomplish this.

from PIL import Image
from imagekit import ImageSpec, register
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit

class LogoProcessor(ImageSpec):
    format = 'JPEG'
    options = {'quality': 90}
    @property
    def processors(self):
        logoimage = self.source
        image = Image.open(logoimage)
        rgb_image = image.convert('RGB')
        r,g,b = rgb_image.getpixel((1, 1))
        return [ResizeToFit(300, 300,mat_color=(r,g,b))]

register.generator('logo_processor', LogoProcessor)

class Company(models.Model):
    company_logo = models.ImageField(upload_to=settings.MEDIA_ROOT,default='')
    company_logo_thumb = ImageSpecField(
            source='company_logo',
            id='logo_processor'
        )
like image 57
Alex Phelps Avatar answered Jul 02 '26 18:07

Alex Phelps