def create_thumbnails(self): """ Crear los thumbnails para todas dimensiones """ initial = log_time("create_thumbnails") sizes = sorted(self.sizes.items(), key=lambda x: x[1][0], reverse=True) last = None for name, dimension in sizes: self.create_thumbnail(name, last=last) last = name assert self.thumbnail_exists(name) log_time("end create_thumbnails", initial=initial)
def create_thumbnail(self, size, last=None): """ Crea el thumbnail para el tamaño *size*. """ initial = log_time("thumbnail: %s" % size, indent=4) # Abrimos la imagen try: name = self.image.name if last is None else \ self.thumbnail_name(last) data = self.image.storage.open(name, 'rb').read() img = Image.open(StringIO(data)) except IOError, e: logging.error('IOError: %s ' % e) return
# Generamos el thumbnail thumb = method.generate(img, width, height) if thumb is not None: thumb_file = ContentFile(thumb.getvalue()) else: thumb_file = ContentFile(data) # Escribimos el thumbnail en el disco name = self.thumbnail_name(size) if self.image.storage.exists(name): self.image.storage.delete(name) self.image.storage.save(name, thumb_file) log_time("end thumbnail", initial=initial, indent=4) def thumbnail_url(self, size): """ Retorna el url del thumbnail de tamaño *size* """ return self.image.storage.url(self.thumbnail_name(size)) def thumbnail_basepath(self): """ Retorna el path donde se almacena el thumbnail. """ return getattr(settings, 'THUMBNAIL_STORAGE_DIR', self.basepath) def thumbnail_name(self, size):