def generate_thumbnail(self, thumbnail_options): """ Return a ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ if not utils.is_storage_local(self.source_storage): # Get a local copy of the source file tmp_image_f = StringIO() orig_f = urllib2.urlopen(self.source_storage.url(self.name)) tmp_image_f.write(orig_f.read()) tmp_image_f.seek(0) self.file = tmp_image_f image = engine.generate_source_image(self, thumbnail_options) thumbnail_image = engine.process_image(image, thumbnail_options) quality = thumbnail_options.get('quality', self.thumbnail_quality) filename = self.get_thumbnail_name(thumbnail_options, transparent=self.is_transparent(thumbnail_image)) data = engine.save_image(thumbnail_image, filename=filename, quality=quality).read() thumbnail = ThumbnailFile(filename, ContentFile(data), storage=self.thumbnail_storage) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_thumbnail(self, thumbnail_options): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ image = engine.generate_source_image(self, thumbnail_options, self.source_generators) thumbnail_image = engine.process_image(image, thumbnail_options, self.thumbnail_processors) quality = thumbnail_options.get('quality', self.thumbnail_quality) filename = self.get_thumbnail_name(thumbnail_options, transparent=self.is_transparent(thumbnail_image)) data = engine.save_image(thumbnail_image, filename=filename, quality=quality).read() thumbnail = ThumbnailFile(filename, ContentFile(data), storage=self.thumbnail_storage) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_thumbnail(self, thumbnail_options): """ Return a ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ image = engine.generate_source_image(self, thumbnail_options) if image is None: raise exceptions.InvalidImageFormatError( "Image file format is not supported!") thumbnail_image = engine.process_image(image, thumbnail_options) quality = thumbnail_options.get('quality', self.thumbnail_quality) filename = self.get_thumbnail_name(thumbnail_options, transparent=self.is_transparent(thumbnail_image)) data = engine.save_image(thumbnail_image, filename=filename, quality=quality).read() thumbnail = ThumbnailFile(filename, ContentFile(data), storage=self.thumbnail_storage) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_thumbnail(self, thumbnail_options, high_resolution=False, silent_template_exception=False): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ thumbnail_options = self.get_options(thumbnail_options) orig_size = thumbnail_options['size'] # remember original size # Size sanity check. min_dim, max_dim = 0, 0 for dim in orig_size: try: dim = int(dim) except (TypeError, ValueError): continue min_dim, max_dim = min(min_dim, dim), max(max_dim, dim) if max_dim == 0 or min_dim < 0: raise exceptions.EasyThumbnailsError( "The source image is an invalid size (%sx%s)" % orig_size) if high_resolution: thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2) image = engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception) if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") icc_profile = image.info.get("icc_profile") thumbnail_image = engine.process_image(image, thumbnail_options, self.thumbnail_processors) if high_resolution: thumbnail_options['size'] = orig_size # restore original size filename = self.get_thumbnail_name( thumbnail_options, transparent=utils.is_transparent(thumbnail_image), high_resolution=high_resolution) quality = thumbnail_options['quality'] subsampling = thumbnail_options['subsampling'] img = engine.save_image( thumbnail_image, filename=filename, quality=quality, subsampling=subsampling, icc_profile=icc_profile) data = img.read() thumbnail = ThumbnailFile( filename, file=ContentFile(data), storage=self.thumbnail_storage, thumbnail_options=thumbnail_options) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_thumbnail(self, thumbnail_options, high_resolution=False, silent_template_exception=False): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ if high_resolution: orig_size = thumbnail_options['size'] # remember original size thumbnail_options = thumbnail_options.copy() thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2) image = engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception) if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") thumbnail_image = engine.process_image(image, thumbnail_options, self.thumbnail_processors) quality = thumbnail_options.get('quality', self.thumbnail_quality) if high_resolution: thumbnail_options['size'] = orig_size # restore original size filename = self.get_thumbnail_name( thumbnail_options, transparent=utils.is_transparent(thumbnail_image), high_resolution=high_resolution) img = engine.save_image( thumbnail_image, filename=filename, quality=quality) data = img.read() thumbnail = ThumbnailFile( filename, file=ContentFile(data), storage=self.thumbnail_storage, thumbnail_options=thumbnail_options) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_source_image(self, thumbnail_options): return engine.generate_source_image(self, thumbnail_options, self.source_generators)
def test_multiple_silent_fail(self): source_generators = [ FakeSourceGenerator(fail=True), FakeSourceGenerator(fail=True)] image = engine.generate_source_image( self.source, {}, source_generators) self.assertEqual(image, None)
def generate_thumbnail(self, thumbnail_options, high_resolution=False, silent_template_exception=False): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ thumbnail_options = self.get_options(thumbnail_options) orig_size = thumbnail_options['size'] # remember original size # Size sanity check. min_dim, max_dim = 0, 0 for dim in orig_size: try: dim = int(dim) except (TypeError, ValueError): continue min_dim, max_dim = min(min_dim, dim), max(max_dim, dim) if max_dim == 0 or min_dim < 0: raise exceptions.EasyThumbnailsError( "The source image is an invalid size (%sx%s)" % orig_size) if high_resolution: thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2) image = engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception) if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") thumbnail_image = engine.process_image(image, thumbnail_options, self.thumbnail_processors) if high_resolution: thumbnail_options['size'] = orig_size # restore original size filename = self.get_thumbnail_name( thumbnail_options, transparent=utils.is_transparent(thumbnail_image), high_resolution=high_resolution) quality = thumbnail_options['quality'] subsampling = thumbnail_options['subsampling'] img = engine.save_image(thumbnail_image, filename=filename, quality=quality, subsampling=subsampling) data = img.read() thumbnail = ThumbnailFile(filename, file=ContentFile(data), storage=self.thumbnail_storage, thumbnail_options=thumbnail_options) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def test_single_silent_fail(self): source_generators = [FakeSourceGenerator(fail=True)] image = engine.generate_source_image(self.source, {}, source_generators) self.assertEqual(image, None)
def generate_thumbnail(self, thumbnail_options, high_resolution=False, silent_template_exception=False): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ thumbnail_options = self.get_options(thumbnail_options) orig_size = thumbnail_options['size'] # remember original size # Size sanity check. min_dim, max_dim = 0, 0 for dim in orig_size: try: dim = int(dim) except (TypeError, ValueError): continue min_dim, max_dim = min(min_dim, dim), max(max_dim, dim) if max_dim == 0 or min_dim < 0: raise exceptions.EasyThumbnailsError( "The source image is an invalid size (%sx%s)" % orig_size) if high_resolution: thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2) image = engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception) if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") thumbnail_image = None IS_GIF = False if isinstance(image, GifImageFile): IS_GIF = True import images2gif gif_image, gif_params = images2gif.readGif(image, False) frames = [] for frame in gif_image: thumbnail_frame = engine.process_image(frame, thumbnail_options, self.thumbnail_processors) frames.append(thumbnail_frame) if thumbnail_image is None: thumbnail_image = frame else: thumbnail_image = engine.process_image(image, thumbnail_options, self.thumbnail_processors) if high_resolution: thumbnail_options['size'] = orig_size # restore original size filename = self.get_thumbnail_name( thumbnail_options, transparent=utils.is_transparent(thumbnail_image), high_resolution=high_resolution) quality = thumbnail_options['quality'] subsampling = thumbnail_options['subsampling'] if IS_GIF: try: # try save with speedup img = images2gif.writeGif(frames, **gif_params) saved = True except: # if not saved, simple save gif_params.update({'subRectangles': False,}) img = images2gif.writeGif(frames, **gif_params) else: img = engine.save_image( thumbnail_image, filename=filename, quality=quality, subsampling=subsampling) data = img.read() thumbnail = ThumbnailFile( filename, file=ContentFile(data), storage=self.thumbnail_storage, thumbnail_options=thumbnail_options) thumbnail.image = thumbnail_image thumbnail._committed = False return thumbnail
def generate_thumbnail(self, thumbnail_options, high_resolution=False, silent_template_exception=False): """ Return an unsaved ``ThumbnailFile`` containing a thumbnail image. The thumbnail image is generated using the ``thumbnail_options`` dictionary. """ thumbnail_options = self.get_options(thumbnail_options) orig_size = thumbnail_options['size'] # remember original size # Size sanity check. min_dim, max_dim = 0, 0 for dim in orig_size: try: dim = int(dim) except (TypeError, ValueError): continue min_dim, max_dim = min(min_dim, dim), max(max_dim, dim) if max_dim == 0 or min_dim < 0: raise exceptions.EasyThumbnailsError( "The source image is an invalid size (%sx%s)" % orig_size) if high_resolution: thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2) image = engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception) if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") self.open() source_image = Image.open(self.file) is_animated_gif = self.name.lower().endswith('.gif') \ and source_image.format == 'GIF' \ and source_image.info.get('duration') != None from io import BytesIO if is_animated_gif: frame_index = 0 palette = source_image.getpalette() from easy_thumbnails.utils import images2gif images = images2gif.readGif(self.file, asNumpy=False) # convert the durations to 0.1 secs durations = [single_duration / 1000.0 for single_duration in images2gif.getDurationsOfGif(self.file)] else: images = [engine.generate_source_image( self, thumbnail_options, self.source_generators, fail_silently=silent_template_exception)] thumbnail_images = [] if high_resolution: thumbnail_options['high_resolution'] = True for image in images: if image is None: raise exceptions.InvalidImageFormatError( "The source file does not appear to be an image") thumb = engine.process_image(image, thumbnail_options, self.thumbnail_processors) thumbnail_images.append(thumb) thumbnail_image = thumbnail_images[0] quality = thumbnail_options.get('quality', self.thumbnail_quality) if high_resolution: thumbnail_options['size'] = orig_size # restore original size del thumbnail_options['high_resolution'] filename = self.get_thumbnail_name( thumbnail_options, transparent=utils.is_transparent(thumbnail_image), high_resolution=high_resolution) quality = thumbnail_options['quality'] subsampling = thumbnail_options['subsampling'] if is_animated_gif: from tempfile import NamedTemporaryFile try: output_temp_file = NamedTemporaryFile('rw', suffix='.gif') images2gif.writeGif(output_temp_file.name, thumbnail_images, duration=durations) output_temp_file = open(output_temp_file.name) data = output_temp_file.read() finally: output_temp_file.close() del output_temp_file else: img = engine.save_image( thumbnail_image, filename=filename, quality=quality) data = img.read() thumbnail = ThumbnailFile( filename, file=ContentFile(data), storage=self.thumbnail_storage, thumbnail_options=thumbnail_options) thumbnail._committed = False return thumbnail