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 get_files_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. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + '...' if is_image(src): if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = src files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src), 'visible_filename': visible_filename, }) return files
def get_files_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. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) if getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None): if is_image(src): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) visible_filename = None else: thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + '...' else: visible_filename = None thumb = src files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src), 'visible_filename': visible_filename, }) return files
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_files_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. """ files = [] 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 files.append({'thumb': thumb, 'src': src, 'is_image': is_image(src)}) return files
def get_files_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. """ files = [] 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 files.append({"thumb": thumb, "src": src, "is_image": is_image(src)}) return files
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 get_files_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. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) backend = getattr(settings, 'CKEDITOR_IMAGE_BACKEND', None) if backend == 'pillow': thumb = utils.get_media_url(utils.get_thumb_filename(filename)) elif backend == 'oss': thumb = default_storage.image_processed_url(filename) else: thumb = src files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src) }) return files
def get_files_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. """ files = [] for filename in get_image_files(user=user): src = utils.get_media_url(filename) visible_filename = None if getattr(settings, "CKEDITOR_IMAGE_BACKEND", None): if is_image(src): thumb = utils.get_media_url(utils.get_thumb_filename(filename)) else: thumb = utils.get_icon_filename(filename) visible_filename = os.path.split(filename)[1] if len(visible_filename) > 20: visible_filename = visible_filename[0:19] + "..." else: thumb = src files.append({"thumb": thumb, "src": src, "is_image": is_image(src), "visible_filename": visible_filename}) return files
def get_files_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. """ files = [] 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 ext = filename.split('.')[-1].lower() fa_icon = 'file' if ext == 'xls' or ext == 'xls': fa_icon = 'excel' elif ext == 'doc' or ext == 'docx': fa_icon = 'word' elif ext == 'ppt' or ext == 'pptx': fa_icon = 'powerpoint' elif ext == 'rar' or ext == 'zip': fa_icon = 'archive' elif ext == 'pdf': fa_icon = 'pdf' elif ext == 'swf': fa_icon = 'video' else: fa_icon = 'image' files.append({ 'thumb': thumb, 'src': src, 'is_image': is_image(src), 'filename': filename.split('/')[-1].split('.')[0].lower(), 'ext': ext, 'icon': fa_icon, }) return files
def _thumbnail_exists(self, image_path): thumb_path = self._to_absolute_path( get_thumb_filename(image_path) ) return os.path.isfile(thumb_path)