def test_jpeg_quality_filter_overrides_setting(self): fil = Filter(spec='width-400|format-jpeg|jpegquality-40') image = Image.objects.create( title="Test image", file=get_temporary_image(), ) f = BytesIO() with patch('PIL.Image.Image.save') as save: fil.run(image, f) save.assert_called_with(f, 'JPEG', icc_profile=None, quality=40, optimize=True, progressive=True)
def test_6_digit_hex(self): fil = Filter(spec='width-400|bgcolor-ffffff') image = Image.objects.create( title="Test image", file=get_temporary_image(), ) out = fil.run(image, BytesIO()) self.assertFalse(out.has_alpha())
def test_gif(self): fil = Filter(spec='width-400|format-gif') image = Image.objects.create( title="Test image", file=get_temporary_image(), ) out = fil.run(image, BytesIO()) self.assertEqual(out.format_name, 'gif')
def test_original_has_alpha(self): # Checks that the test image we're using has alpha fil = Filter(spec='width-400') image = Image.objects.create( title="Test image", file=get_temporary_image(), ) out = fil.run(image, BytesIO()) self.assertTrue(out.has_alpha())
def get_rendition(self, filter): if isinstance(filter, str): filter = Filter(spec=filter) cache_key = filter.get_cache_key(self) try: rendition = self.renditions.get( filter_spec=filter.spec, focal_point_key=cache_key ) except ObjectDoesNotExist: # Generate the rendition image generated_image = filter.run(self, BytesIO()) # Generate filename input_filename = os.path.basename(self.file.name) input_filename_without_extension, input_extension = os.path.splitext(input_filename) format_mapping = { 'jpeg': '.jpg', 'png': '.png', 'gif': '.gif', } # Get the file extension to output output_extension = filter.spec.replace('|', '.') + format_mapping[generated_image.format_name] # Truncate filename to prevent it going over 60 chars output_filename_without_extension = input_filename_without_extension[:(59 - len(output_extension))] output_filename = output_filename_without_extension + '.' + output_extension rendition, created = self.renditions.get_or_create( filter_spec=filter.spec, focal_point_key=cache_key, defaults={'file': File(generated_image.f, name=output_filename)} ) return rendition