parent
1e753eec6b
commit
29f80b3f64
9 changed files with 61 additions and 6 deletions
@ -0,0 +1,49 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
""" |
||||||
|
Sorl Thumbnail Engine that create special mailing thumbnails |
||||||
|
""" |
||||||
|
from PIL import Image # , ImageColor |
||||||
|
from sorl.thumbnail.engines.pil_engine import Engine |
||||||
|
|
||||||
|
from django.contrib.staticfiles import finders |
||||||
|
|
||||||
|
russia_size = (89, 94) |
||||||
|
foreign_size = (110, 110) |
||||||
|
russia_background = finders.find('newsletter/images/dailymail_russia_background.png') |
||||||
|
foreign_background = finders.find('newsletter/images/dailymail_foreign_background.png') |
||||||
|
hit_mark = finders.find('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 == "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) |
||||||
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
Loading…
Reference in new issue