def process_image(self, full_file_path, create_thumbnail=False, create_carousel=False): # Get the pieces of the file and path that # we need to process the file. folder_root_path = os.path.dirname(full_file_path) ext = get_ext(full_file_path) orig_file_name = get_media_file_name(ext) carousel_file_name = orig_file_name.replace('.' + ext, '_carousel.{}'.format(ext)) thumb_file_name = orig_file_name.replace('.' + ext, '_thumb.{}'.format(ext)) orig_full_file_path = os.path.join(folder_root_path, self.DEFAULT_ORIG_FOLDER_NAME, orig_file_name) create_folder_path(orig_full_file_path, contains_file_name=True) shutil.copy2(full_file_path, orig_full_file_path) if create_carousel: carousel_full_file_path = os.path.join(folder_root_path, self.DEFAULT_CAROUSEL_FOLDER_NAME, carousel_file_name) create_folder_path(carousel_full_file_path, contains_file_name=True) shutil.copy2(full_file_path, carousel_full_file_path) img = ImageWrapper(carousel_full_file_path) if self.verbose: print('==== Before Create Carousel ====') print(img.get_image_info()) img = self.resize_image(carousel_full_file_path, new_width=None, new_height=self.DEFAULT_CAROUSEL_SIZE[1], antialias=True) img = self.overlay_image_on_white_background(carousel_full_file_path, self.DEFAULT_CAROUSEL_SIZE[0], self.DEFAULT_CAROUSEL_SIZE[1], centered=True) img = self.reduce_image(carousel_full_file_path, ImageWrapper.DEFAULT_JPEG_QUALITY_WEB) if self.verbose: print('==== After Create Carousel ====') print(img.get_image_info()) if create_thumbnail: thumb_full_file_path = os.path.join(folder_root_path, self.DEFAULT_THUMBNAIL_FOLDER_NAME, thumb_file_name) create_folder_path(thumb_full_file_path, contains_file_name=True) shutil.copy2(full_file_path, thumb_full_file_path) img = ImageWrapper(thumb_full_file_path) if self.verbose: print('==== Before Create Thumbnail ====') print(img.get_image_info()) if img.height > img.width: img = self.resize_image(thumb_full_file_path, new_width=self.DEFAULT_THUMBNAIL_SIZE[0], new_height=None, antialias=True) else: img = self.resize_image(thumb_full_file_path, new_width=None, new_height=self.DEFAULT_THUMBNAIL_SIZE[1], antialias=True) img = self.crop_image(thumb_full_file_path, self.DEFAULT_THUMBNAIL_SIZE, from_center=True) img = self.reduce_image(thumb_full_file_path, ImageWrapper.DEFAULT_JPEG_QUALITY_WEB) if self.verbose: print('==== After Create Thumbnail ====') print(img.get_image_info()) os.remove(full_file_path)
def init(self, full_file_path): self.full_file_path = full_file_path self.folder_path = os.path.dirname(full_file_path) self.file_name = os.path.basename(full_file_path) self.ext = get_ext(self.full_file_path) self._img = Image.open(self.full_file_path) self.width = self._img.size[0] self.height = self._img.size[1] self.file_size = os.stat(full_file_path).st_size