def test_generate_filename(self): self.assertEqual( generate_filename(SourceFile('url'), '100x200', 'center'), ['0af', 'a360db703bd5c2fe7c83843ce7738a0a6d37b']) self.assertEqual( generate_filename(SourceFile('url'), '200x200', 'center'), ['851', '521c21fe9709802e9d4eb20a5fe84c18cd3ad'])
def test_django_image_files(self): from django.db.models.fields import files field = files.FileField() f = SourceFile(files.FieldFile(field=field, instance=None, name=self.FILE_PATH)) self.assertEqual(f.file, self.FILE_PATH) f = SourceFile(files.ImageFieldFile(field=field, instance=None, name=self.FILE_PATH)) self.assertEqual(f.file, self.FILE_PATH)
def test_base64_encoded_string(self): file = SourceFile(data.BASE64_STRING_OF_IMAGE) self.assertEqual( hashlib.sha1(file.open().getvalue()).hexdigest(), '6666212f5302426c845ecb2a2901fae021735f24' ) image = Image.open(BytesIO(file.open().read())) self.assertIsNotNone(image.load())
def setUp(self): self.engine = self.ENGINE() self.filename = os.path.join(os.path.dirname(__file__), 'test_image.jpg') self.file = SourceFile(self.filename) self.url = SourceFile('http://puppies.lkng.me/400x600/') image = Image.new('L', (400, 600)) image.save(self.filename)
def test_engine_get_format(self): image = self.engine.engine_load_image(self.file) self.assertEqual(self.engine.engine_get_format(image), 'JPEG') png_path = os.path.join(os.path.dirname(__file__), 'test_image.png') Image.new('L', (400, 600)).save(png_path) image = self.engine.engine_load_image(SourceFile(png_path)) self.assertEqual(self.engine.engine_get_format(image), 'PNG') os.remove(png_path) png_path = os.path.join(os.path.dirname(__file__), 'test_image.gif') Image.new('L', (400, 600)).save(png_path) image = self.engine.engine_load_image(SourceFile(png_path)) self.assertEqual(self.engine.engine_get_format(image), 'GIF') os.remove(png_path)
def get_thumbnail(original, size, **options): """ Creates or gets an already created thumbnail for the given image with the given size and options. :param original: File-path, url or base64-encoded string of the image that you want an thumbnail. :param size: String with the wanted thumbnail size. On the form: ``200x200``, ``200`` or ``x200``. :param crop: Crop settings, should be ``center``, ``top``, ``right``, ``bottom``, ``left``. :param force: If set to ``True`` the thumbnail will be created even if it exists before. :param quality: Overrides ``THUMBNAIL_QUALITY``, will set the quality used by the backend while saving the thumbnail. :param scale_up: Overrides ``THUMBNAIL_SCALE_UP``, if set to ``True`` the image will be scaled up if necessary. :param colormode: Overrides ``THUMBNAIL_COLORMODE``, The default colormode for thumbnails. Supports all values supported by pillow. In other engines there is a best effort translation from pillow modes to the modes supported by the current engine. :param format: Overrides the format the thumbnail will be saved in. This will override both the detected file type as well as the one specified in ``THUMBNAIL_FALLBACK_FORMAT``. :return: A Thumbnail object """ engine = get_engine() cache = get_cache_backend() original = SourceFile(original) crop = options.get('crop', None) options = engine.evaluate_options(options) thumbnail_name = generate_filename(original, size, crop) if settings.THUMBNAIL_DUMMY: engine = DummyEngine() return engine.get_thumbnail(thumbnail_name, engine.parse_size(size), crop, options) cached = cache.get(thumbnail_name) force = options is not None and 'force' in options and options['force'] if not force and cached: return cached thumbnail = Thumbnail(thumbnail_name, engine.get_format(original, options)) if force or not thumbnail.exists: size = engine.parse_size(size) thumbnail.image = engine.get_thumbnail(original, size, crop, options) thumbnail.save(options) for resolution in settings.THUMBNAIL_ALTERNATIVE_RESOLUTIONS: resolution_size = engine.calculate_alternative_resolution_size( resolution, size) image = engine.get_thumbnail(original, resolution_size, crop, options) thumbnail.save_alternative_resolution(resolution, image, options) cache.set(thumbnail) return thumbnail
def test_init(self): self.assertEqual(SourceFile(self.FILE_PATH).file, self.FILE_PATH)
def setUp(self): self.engine = DummyEngine() self.filename = os.path.join(os.path.dirname(__file__), 'test_image.jpg') self.file = SourceFile(self.filename) self.url = SourceFile('http://puppies.lkng.me/400x600/')