def create_thumbnail(file_path): thumbnail_filename = utils.get_thumb_filename(file_path) thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1]) file_format = thumbnail_format.split('/')[1] image = default_storage.open(file_path) image = Image.open(image) # Convert to RGB if necessary # Thanks to Limodou on DjangoSnippets.org # http://www.djangosnippets.org/snippets/20/ if image.mode not in ('L', 'RGB'): image = image.convert('RGB') # scale and crop to thumbnail imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS) thumbnail_io = BytesIO() imagefit.save(thumbnail_io, format=file_format) thumbnail = InMemoryUploadedFile(thumbnail_io, None, thumbnail_filename, thumbnail_format, len(thumbnail_io.getvalue()), None) thumbnail.seek(0) return default_storage.save(thumbnail_filename, thumbnail)
def create_thumbnail(file_path): thumbnail_filename = utils.get_thumb_filename(file_path) thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1]) file_format = thumbnail_format.split('/')[1] image = default_storage.open(file_path) image = Image.open(image) # Convert to RGB if necessary # Thanks to Limodou on DjangoSnippets.org # http://www.djangosnippets.org/snippets/20/ if image.mode not in ('L', 'RGB'): image = image.convert('RGB') # scale and crop to thumbnail imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS) thumbnail_io = BytesIO() imagefit.save(thumbnail_io, format=file_format) thumbnail = InMemoryUploadedFile( thumbnail_io, None, thumbnail_filename, thumbnail_format, len(thumbnail_io.getvalue()), None) thumbnail.seek(0) return default_storage.save(thumbnail_filename, thumbnail)
def handle_noargs(self, **options): if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): backend = get_backend() for image in get_image_files(): if not os.path.isfile(get_thumb_filename(image)): self.stdout.write("Creating thumbnail for {0}".format(image)) try: backend.create_thumbnail(image) except Exception as e: self.stdout.write("Couldn't create thumbnail for {0}: {1}".format(image, e)) self.stdout.write("Finished") else: self.stdout.write("No thumbnail backend is enabled")
def get_image_browse_urls(user=None): """ Recursively walks all dirs under upload dir and generates a list of thumbnail and full image URL's for each file found. """ images = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = src images.append({'thumb': thumb, 'src': src}) return images
def handle_noargs(self, **options): if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): backend = get_backend() for image in get_image_files(): if not os.path.isfile(get_thumb_filename(image)): self.stdout.write( "Creating thumbnail for {0}".format(image)) try: backend.create_thumbnail(image) except Exception as e: self.stdout.write( "Couldn't create thumbnail for {0}: {1}".format( image, e)) self.stdout.write("Finished") else: self.stdout.write("No thumbnail backend is enabled")
def get_image_browse_urls(user=None): """ Recursively walks all dirs under upload dir and generates a list of thumbnail and full image URL's for each file found. """ images = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = src images.append({ 'thumb': thumb, 'src': src }) return images