You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
2.3 KiB

# -*- coding: utf-8 -*-
"""
Sorl Thumbnail Engine that create special mailing thumbnails
"""
import os
from PIL import Image
from sorl.thumbnail.engines.pil_engine import Engine
from django.contrib.staticfiles import finders
from django.conf import settings
from sorl.thumbnail.conf import settings as solr_settings
russia_size = (89, 94)
foreign_size = (110, 110)
russia_background = os.path.join(settings.SITE_ROOT, 'static/newsletter/images/dailymail_russia_background.png')
foreign_background = os.path.join(settings.SITE_ROOT, 'static/newsletter/images/dailymail_foreign_background.png')
hit_mark = os.path.join(settings.SITE_ROOT, 'static/newsletter/images/hitmark.png')
class SorlEngine(Engine):
def create_dailymail_thumb(self, image, size, background, hit=False):
# берем фон
thumb = Image.open(background)
# берем ресайз картинки
image.thumbnail(size, resample=4)
# считаем положение
box = (
(thumb.width - image.width) / 2, # left
(thumb.height - image.height) / 2, # top
(thumb.width - image.width) / 2 + image.width, # right
(thumb.height - image.height) / 2 + image.height # bottom
)
if image.mode == "P" and 'transparency' in image.info:
image = image.convert('RGBA')
# вставляем
if image.mode == "RGBA":
# с прозрачностью
thumb.paste(image, box, image)
else:
# без
thumb.paste(image, box)
# вставляем отметку "ХИТ"
if hit:
hit_mark_image = Image.open(hit_mark)
thumb.paste(hit_mark_image, (0, 0), hit_mark_image)
return thumb
def create(self, image, geometry, options):
if options.get('dailymail') in ['russia', 'moscow']:
return self.create_dailymail_thumb(image, russia_size, russia_background, options.get('hit', False))
if options.get('dailymail') == 'foreign':
return self.create_dailymail_thumb(image, foreign_size, foreign_background, options.get('hit', False))
return super(SorlEngine, self).create(image, geometry, options)
def write(self, image, options, thumbnail):
options['image_info'] = image.info
super(SorlEngine, self).write(image, options, thumbnail)