def test_16_image_process_format(self): """Test the format parameter of image_process.""" image = tools.base64_to_image( tools.image_process(self.base64_1920x1080_jpeg, output_format='PNG')) self.assertEqual(image.format, 'PNG', "change format to PNG") image = tools.base64_to_image( tools.image_process(self.base64_1x1_png, output_format='JpEg')) self.assertEqual(image.format, 'JPEG', "change format to JPEG (case insensitive)") image = tools.base64_to_image( tools.image_process(self.base64_1920x1080_jpeg, output_format='BMP')) self.assertEqual(image.format, 'PNG', "change format to BMP converted to PNG") self.base64_image_1080_1920_rgba = tools.image_to_base64( Image.new('RGBA', (108, 192)), 'PNG') image = tools.base64_to_image( tools.image_process(self.base64_image_1080_1920_rgba, output_format='jpeg')) self.assertEqual(image.format, 'JPEG', "change format PNG with RGBA to JPEG") # pass quality to force the image to be processed self.base64_image_1080_1920_tiff = tools.image_to_base64( Image.new('RGB', (108, 192)), 'TIFF') image = tools.base64_to_image( tools.image_process(self.base64_image_1080_1920_tiff, quality=95)) self.assertEqual(image.format, 'JPEG', "unsupported format to JPEG")
def setUp(self): super(TestImage, self).setUp() self.bg_color = (135, 90, 123) self.fill_color = (0, 160, 157) self.base64_1x1_png = b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC' self.base64_svg = base64.b64encode(b'<svg></svg>') self.base64_1920x1080_jpeg = tools.image_to_base64( Image.new('RGB', (1920, 1080)), 'JPEG') # Draw a red square in the middle of the image, this will be used to # verify crop is working. The border is going to be `self.bg_color` and # the middle is going to be `self.fill_color`. # horizontal image (border is left/right) image = Image.new('RGB', (1920, 1080), color=self.bg_color) offset = (image.size[0] - image.size[1]) / 2 draw = ImageDraw.Draw(image) draw.rectangle(xy=[(offset, 0), (image.size[0] - offset, image.size[1])], fill=self.fill_color) self.base64_1920x1080_png = tools.image_to_base64(image, 'PNG') # vertical image (border is top/bottom) image = Image.new('RGB', (1080, 1920), color=self.bg_color) offset = (image.size[1] - image.size[0]) / 2 draw = ImageDraw.Draw(image) draw.rectangle(xy=[(0, offset), (image.size[0], image.size[1] - offset)], fill=self.fill_color) self.base64_1080x1920_png = tools.image_to_base64(image, 'PNG')
def test_12_image_process_verify_resolution(self): """Test the verify_resolution parameter of image_process.""" res = tools.image_process(self.base64_1920x1080_jpeg, verify_resolution=True) self.assertNotEqual(res, False, "size ok") base64_image_excessive = tools.image_to_base64( Image.new('RGB', (45001, 1000)), 'PNG') with self.assertRaises(ValueError, msg="size excessive"): tools.image_process(base64_image_excessive, verify_resolution=True)
def test_13_image_process_quality(self): """Test the quality parameter of image_process.""" # CASE: PNG RGBA doesn't apply quality, just optimize image = tools.image_to_base64(Image.new('RGBA', (1080, 1920)), 'PNG') res = tools.image_process(image) self.assertLessEqual(len(res), len(image)) # CASE: PNG RGB doesn't apply quality, just optimize image = tools.image_to_base64(Image.new('P', (1080, 1920)), 'PNG') res = tools.image_process(image) self.assertLessEqual(len(res), len(image)) # CASE: JPEG optimize + reduced quality res = tools.image_process(self.base64_1920x1080_jpeg) self.assertLessEqual(len(res), len(self.base64_1920x1080_jpeg)) # CASE: GIF doesn't apply quality, just optimize image = tools.image_to_base64(Image.new('RGB', (1080, 1920)), 'GIF') res = tools.image_process(image) self.assertLessEqual(len(res), len(image))
def test_15_image_process_colorize(self): """Test the colorize parameter of image_process.""" # verify initial condition image_rgba = Image.new('RGBA', (1, 1)) self.assertEqual(image_rgba.mode, 'RGBA') self.assertEqual(image_rgba.getpixel((0, 0)), (0, 0, 0, 0)) base64_rgba = tools.image_to_base64(image_rgba, 'PNG') # CASE: color random, color has changed image = tools.base64_to_image( tools.image_process(base64_rgba, colorize=True)) self.assertEqual(image.mode, 'RGB') self.assertNotEqual(image.getpixel((0, 0)), (0, 0, 0))
def _set_images(self): for fname in self._get_images_for_test(): fname_split = fname.split('.') if not fname_split[0] in _file_cache: with Image.open(os.path.join(dir_path, fname), 'r') as img: base64_img = image_to_base64(img, 'JPEG') primary, secondary = self.env['base.document.layout'].create( {})._parse_logo_colors(base64_img) _img = frozendict({ 'img': base64_img, 'colors': { 'primary_color': primary, 'secondary_color': secondary, }, }) _file_cache[fname_split[0]] = _img self.company_imgs = frozendict(_file_cache)
def _get_exif_colored_square_b64(self, orientation, colors, size): image = Image.new('RGB', (size, size), color=self.bg_color) draw = ImageDraw.Draw(image) # Paint the colors on the 4 corners, to be able to test which colors # move on which corners. draw.rectangle(xy=[(0, 0), (size // 2, size // 2)], fill=colors[0]) # top/left draw.rectangle(xy=[(size // 2, 0), (size, size // 2)], fill=colors[1]) # top/right draw.rectangle(xy=[(0, size // 2), (size // 2, size)], fill=colors[2]) # bottom/left draw.rectangle(xy=[(size // 2, size // 2), (size, size)], fill=colors[3]) # bottom/right # Set the proper exif tag based on orientation params. exif = b'Exif\x00\x00II*\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00' + bytes( [orientation]) + b'\x00\x00\x00\x00\x00\x00\x00' # The image image is saved with the exif tag. return tools.image_to_base64(image, 'JPEG', exif=exif)
def test_01_website_favicon(self): """The goal of this test is to make sure the favicon is correctly handled on the website.""" # Test setting an Ico file directly, done through create Website = self.env['website'] website = Website.create({ 'name': 'Test Website', 'favicon': Website._default_favicon(), }) image = base64_to_image(website.favicon) self.assertEqual(image.format, 'ICO') # Test setting a JPEG file that is too big, done through write bg_color = (135, 90, 123) image = Image.new('RGB', (1920, 1080), color=bg_color) website.favicon = image_to_base64(image, 'JPEG') image = base64_to_image(website.favicon) self.assertEqual(image.format, 'ICO') self.assertEqual(image.size, (256, 256)) self.assertEqual(image.getpixel((0, 0)), bg_color)
def test_01_image_to_base64(self): """Test that a PIL image is correctly saved as base64.""" image = Image.new('RGB', (1, 1)) image_base64 = tools.image_to_base64(image, 'PNG') self.assertEqual(image_base64, self.base64_1x1_png)