def test_doesnt_alter_input_paste_image(self): """Resizing should be done on a copy, not the original.""" base_image = Images._create_blank_image(500, 500, 'red') paste_image = Images._create_blank_image(600, 600, 'blue') expected = (600, 600) Images._paste_image( base_image=base_image, pixels_from_top=20, paste_image=paste_image, pixels_before=20, max_height=50) observed = paste_image.size self.assertEqual(expected, observed)
def test_shrink_square_to_available_with_y_offset(self): """Should resize to fit available space.""" base_image = Images._create_blank_image(500, 500, 'red') pixels_from_top = 50 paste_image = Images._create_blank_image(600, 600, 'blue') pixels_before = 5 expected = 490 base_image, observed = Images._paste_image( base_image=base_image, pixels_from_top=pixels_from_top, paste_image=paste_image, pixels_before=pixels_before, max_height=None) self.assertEqual(expected, observed)
def test_shrink_square_to_available(self): """Should resize to width.""" pixels_from_top = 0 base_image = Images._create_blank_image(500, 500, 'red') paste_image = Images._create_blank_image(600, 600, 'blue') pixels_before = 5 expected = 485 base_image, observed = Images._paste_image( base_image=base_image, pixels_from_top=pixels_from_top, paste_image=paste_image, pixels_before=pixels_before, max_height=None) self.assertEqual(expected, observed)
def test_shrink_tall_to_max_height_with_y_offset(self): """Should resize to max_height.""" base_image = Images._create_blank_image(500, 500, 'red') pixels_from_top = 100 paste_image = Images._create_blank_image(750, 600, 'blue') pixels_before = 5 max_height = 250 expected = 355 base_image, observed = Images._paste_image( base_image=base_image, pixels_from_top=pixels_from_top, paste_image=paste_image, pixels_before=pixels_before, max_height=max_height) self.assertEqual(expected, observed)
def test_prepare_base_image_no_logo(self): """Should return a base image with no logo.""" settings = {'image_width': 500, 'image_height': 500, 'image_color': 'white', 'logo_image_path': ''} observed, _ = Images._prepare_base_image(settings=settings, xlsform_path="") expected = Images._create_blank_image(500, 500, 'white') self.assertEqual(list(expected.getdata()), list(observed.getdata()))
def test_line_exceeds_width_and_height_multiple(self): """Should return an errors indicating which lines are too big.""" base_image = Images._create_blank_image(50, 100, 'white') text = ['This text is too big', 'and so is this'] settings = {'text_label_font_name': 'arialbd.ttf', 'text_label_font_size': 64, 'text_label_font_color': 'red'} font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='label') image_log = logging.getLogger('odk_tools.question_images.images') with self.assertLogs(logger=image_log, level="WARN") as logs: Images._draw_text( base_image=base_image, pixels_from_top=0, pixels_before=10, pixels_between=5, **font_kwargs, text=text, image_name="img") expected = 3 self.assertEqual(expected, len(logs.output))
def test_line_exceeds_height(self): """Should return an error indicating the line that was too tall.""" base_image = Images._create_blank_image(200, 100, 'white') text = ['Big', 'Text'] settings = {'text_label_font_name': 'arialbd.ttf', 'text_label_font_size': 64, 'text_label_font_color': 'red'} font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='label') image_log = logging.getLogger('odk_tools.question_images.images') with self.assertLogs(logger=image_log, level="WARN") as logs: Images._draw_text( base_image=base_image, pixels_from_top=0, pixels_before=10, pixels_between=5, **font_kwargs, text=text, image_name="img") expected = "WARNING:odk_tools.question_images.images:" \ "Text outside image margins. image name (img)," \ " dim (height), pos (59), text ({0})".format(text[1]) self.assertEqual(expected, logs.output[0])
def test_line_exceeds_width_and_height(self): """Should return an errors indicating the line is too wide and tall.""" base_image = Images._create_blank_image(50, 50, 'white') text = ['This is too big'] settings = {'text_label_font_name': 'arialbd.ttf', 'text_label_font_size': 64, 'text_label_font_color': 'red'} font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='label') image_log = logging.getLogger('odk_tools.question_images.images') with self.assertLogs(logger=image_log, level="WARN") as logs: Images._draw_text( base_image=base_image, pixels_from_top=0, pixels_before=10, pixels_between=5, **font_kwargs, text=text, image_name="img") self.assertEqual(2, len(logs.output)) expected_width = "dim (width), pos (436), text ({0})".format(text[0]) self.assertTrue(logs.output[0].endswith(expected_width)) expected_height = "dim (height), pos (72), text ({0})".format(text[0]) self.assertTrue(logs.output[1].endswith(expected_height))
def test_ideal_input(self): """Should return a modified image with expected offset and no errors.""" settings = ImageSettings.read(xlsform_workbook=self.xlsform1_workbook) settings = settings[2] base_image = Images._create_blank_image( settings['image_width'], settings['image_height'], settings['image_color']) original_image = base_image.copy() text = ['This is some text', 'that is for a question'] font_kwargs = ImageSettings._get_font_kwargs( settings=settings, label_or_hint='label') with patch('logging.Logger.warn', MagicMock()) as log_warn: drawn_image, vertical = Images._draw_text( base_image=base_image, pixels_from_top=0, pixels_before=10, pixels_between=5, **font_kwargs, text=text, image_name='img') self.assertFalse(log_warn.called) self.assertNotEqual(list(original_image.getdata()), list(drawn_image.getdata())) self.assertEqual(79, vertical)
def test_create_blank_image(self): """Should create an image of the expected size and colour.""" observed = Images._create_blank_image(50, 10, 'lavender') self.assertEqual((50, 10), observed.size) lavender_rgb_values = (230, 230, 250) self.assertEqual(lavender_rgb_values, observed.load()[5, 5])