Skip to content
Snippets Groups Projects
Commit 1b9b9ac3262b authored by Tom Gottfried's avatar Tom Gottfried
Browse files

Add test for keep_ratio without fixed width or height.

parent 4034e1bbc5fa
No related branches found
No related tags found
1 merge request!45Keep aspect ratio of images if width or height is not known
File added
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import datetime import datetime
import os import os
import re
import unittest import unittest
import zipfile import zipfile
import traceback import traceback
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
from io import BytesIO from io import BytesIO
from genshi.template import TemplateError from genshi.template import TemplateError
from PIL import Image
from pyjon.utils import get_secure_filename from pyjon.utils import get_secure_filename
from py3o.template import Template, TextTemplate, TemplateException from py3o.template import Template, TextTemplate, TemplateException
...@@ -17,7 +19,8 @@ ...@@ -17,7 +19,8 @@
from pyjon.utils import get_secure_filename from pyjon.utils import get_secure_filename
from py3o.template import Template, TextTemplate, TemplateException from py3o.template import Template, TextTemplate, TemplateException
from py3o.template.main import XML_NS, get_soft_breaks, MANIFEST from py3o.template.main import (
XML_NS, get_image_frames, get_soft_breaks, MANIFEST)
if six.PY3: if six.PY3:
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
...@@ -774,6 +777,70 @@ ...@@ -774,6 +777,70 @@
self.assertFalse(error) self.assertFalse(error)
def test_image_keep_ratio(self):
"""Test keep_ratio parameter with insertion of images"""
template_name = pkg_resources.resource_filename(
'py3o.template',
'tests/templates/py3o_image_keep_ratio.odt'
)
image_name = pkg_resources.resource_filename(
'py3o.template',
'tests/templates/images/new_logo.png'
)
image = open(image_name, 'rb').read()
pil_img = Image.open(BytesIO(image))
img_ratio = pil_img.size[0] / float(pil_img.size[1])
outname = get_secure_filename()
template = Template(template_name, outname)
nmspc = template.namespaces
image_frames = {
elem.get('{%s}name' % nmspc['draw']):
elem for elem in get_image_frames(template.content_trees[0], nmspc)
}
data_dict = {
'image': base64.b64encode(image)
}
template.render(data_dict)
outodt = zipfile.ZipFile(outname, 'r')
image_entries = get_image_frames(
lxml.etree.parse(
BytesIO(outodt.read(template.templated_files[0]))),
nmspc
)
for frame in image_entries:
name = frame.get('{%s}name' % nmspc['draw'])
tframe = image_frames[name]
if 'keep_ratio=False' in name:
# result dimension should equal template
for dim in ['width', 'height']:
self.assertEqual(
frame.get('{%s}%s' % (nmspc['svg'], dim)),
tframe.get('{%s}%s' % (nmspc['svg'], dim)),
)
else:
# Compare frame aspect ratio with that of image and check that
# frame dimensions do not exceed those of the placeholders
height = float(re.sub(
'[a-zA-Z]+', '', frame.get('{%s}height' % nmspc['svg'])))
width = float(re.sub(
'[a-zA-Z]+', '', frame.get('{%s}width' % nmspc['svg'])))
frame_ratio = width / height
self.assertAlmostEqual(frame_ratio, img_ratio)
theight = float(re.sub(
'[a-zA-Z]+', '', tframe.get('{%s}height' % nmspc['svg'])))
twidth = float(re.sub(
'[a-zA-Z]+', '', tframe.get('{%s}width' % nmspc['svg'])))
self.assertLessEqual(height, theight)
self.assertLessEqual(width, twidth)
def test_text_template(self): def test_text_template(self):
template_name = pkg_resources.resource_filename( template_name = pkg_resources.resource_filename(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment