48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from papiezator.models import Pope, PerfectPope, decimal_places
|
|
from papiezator.exceptions import ENOPopeException
|
|
from PIL import Image
|
|
from decimal import Decimal
|
|
PREFIX = "popes"
|
|
|
|
|
|
|
|
|
|
def select_best_pope(aspect_ratio):
|
|
pp = PerfectPope.objects.filter(aspect_ratio=aspect_ratio)
|
|
if pp:
|
|
return pp[0].pope
|
|
lte = Pope.objects.filter(aspect_ratio__lte=aspect_ratio).order_by('-aspect_ratio')
|
|
gte = Pope.objects.filter(aspect_ratio__gte=aspect_ratio).order_by('aspect_ratio')
|
|
|
|
if not gte and not lte:
|
|
raise ENOPopeException
|
|
elif not gte:
|
|
return lte[0].path
|
|
elif not lte:
|
|
return gte[0].path
|
|
else:
|
|
aspect_ratio = Decimal(aspect_ratio)
|
|
r_lte = abs(aspect_ratio - lte[0].aspect_ratio) # FIXME:
|
|
r_gte = abs(aspect_ratio - gte[0].aspect_ratio)
|
|
if r_lte >= r_gte:
|
|
return _select_pope(aspect_ratio, gte[0])
|
|
else:
|
|
return _select_pope(aspect_ratio, lte[0])
|
|
|
|
|
|
def _select_pope(aspect_ratio, pope):
|
|
pp = PerfectPope(aspect_ratio=aspect_ratio, pope=pope)
|
|
pp.save() # FIXME:
|
|
return pope
|
|
|
|
|
|
class PopeMaster:
|
|
def __init__(self):
|
|
pass
|
|
|
|
def parse_pope(self, pope):
|
|
im = Image.open(pope)
|
|
width, height = im.size
|
|
aspect_ratio = round(width/height, decimal_places)
|
|
return Pope(path=pope, width=width, height=height, aspect_ratio=aspect_ratio)
|