def populate_database(db): sample_keyword = Keyword('landscape') db.session.add(sample_keyword) sample_image = Image('yolo.jpg', 'yolo', datetime.datetime.utcnow(), 'Westeros', [sample_keyword]) if not os.path.exists(get_full_path('yolo.jpg')): os.mknod(get_full_path('yolo.jpg')) db.session.add(sample_image) db.session.commit()
def create_thumbnail(image, min_dimension): image_full_path = get_full_path(image.original_path) width, height = ImageGenerator.calculate_dimensions( image_full_path, min_dimension, False) thumbnail_name = ImageGenerator.create_thumbnail_file( image_full_path, width, height, min_dimension) print('Thumbnail: {}x{}, Destination: {}'.format( width, height, get_full_path(thumbnail_name))) thumbnail = ThumbnailImage(thumbnail_name, image) return thumbnail
def create_downsampled(image, max_dimension): image_full_path = get_full_path(image.original_path) width, height = ImageGenerator.calculate_dimensions( image_full_path, max_dimension, True) downsampled_name = ImageGenerator.create_downsampled_file( image_full_path, width, height) print('Downsampled: {}x{}, Destination: {}'.format( width, height, get_full_path(downsampled_name))) downsampled_image = DownsampledImage(downsampled_name, image) return downsampled_image
def create_gallery_webpage(album, portfolio_id, build_dir): # copy all the thumbnail images in the album to a new folder with tempfile.TemporaryDirectory() as temp_images_dir: for image in album.images: shutil.copy(get_full_path(image.thumbnail_image.path), temp_images_dir) album_sprites_dir = os.path.join(build_dir, album.name + '_sprites') shutil.rmtree(album_sprites_dir, ignore_errors=True) os.mkdir(album_sprites_dir) album_css_dir = os.path.join(build_dir, album.name + '_css') shutil.rmtree(album_css_dir, ignore_errors=True) os.mkdir(album_css_dir) # run the glue spritemap generation - outputs are spritemap and css code create_spritemaps(temp_images_dir, album_sprites_dir, album_css_dir, album) # generate the page HTML hash_string = os.path.basename(temp_images_dir) generate_html(build_dir, album, Portfolio.query.get(portfolio_id).albums, hash_string) shutil.copytree(album_css_dir, os.path.join(build_dir, 'css'), dirs_exist_ok=True) shutil.copytree(album_sprites_dir, os.path.join(build_dir, 'sprites'), dirs_exist_ok=True)
def create_downsampled_file(image_full_path, width, height): downscaled_name = ImageGenerator.get_downscaled_file_name( image_full_path, width, height) convert_cmd = "convert -strip -interlace Plane -quality 85% \"{}\" -resize {}x{} \"{}\"".format( image_full_path, width, height, get_full_path(downscaled_name)) subprocess.check_output(convert_cmd, shell=True) return downscaled_name
def create_thumbnail_file(image_full_path, scaled_width, scaled_height, square_size): thumbnail_name = ImageGenerator.get_downscaled_file_name( image_full_path, square_size, square_size) convert_cmd = "convert -quality 70% -resize {}x{}^ -extent {}x{} -gravity Center \( \"{}\" -strip -resize {}x{} \) \"{}\"".format( square_size, square_size, square_size, square_size, image_full_path, scaled_width, scaled_height, get_full_path(thumbnail_name)) subprocess.check_output(convert_cmd, shell=True) return thumbnail_name
def test_delete_image(self): populate_database(db) image_id = 1 image = Image.query.get(image_id) original_image_full_path = get_full_path(image.original_path) response = self.client.delete('/api/v1/images/{}'.format(image_id)) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(Image.query.get(image_id), None) self.assertEquals(os.path.exists(original_image_full_path), False)
def __init__(self, image_name, original_image): self.path = image_name self.original_image = original_image width, height = PilImage.open(get_full_path(image_name)).size self.dimensions = '{}x{}'.format(width, height)
def api_generate_website(portfolio_id): thumbnail_size = 400 downsample_max_size = 2400 # downsample_size images = get_all_images_in_portfolio(portfolio_id) image_count = len(images) # create thumbnail and downsampled images for all images in the albums for idx, image in enumerate(images): if not image.thumbnail_image: image.generate_thumbnail(thumbnail_size) if not image.downsampled_image: image.generate_downsampled(downsample_max_size) percent = ((idx + 1) / image_count) * 100.0 print('Derived Image Generation: {:.2f}% - ({} of {})'.format( percent, idx + 1, image_count)) portfolio = Portfolio.query.get(portfolio_id) with tempfile.TemporaryDirectory() as temp_build_dir: for directory in ['css', 'js', 'sprites']: os.mkdir(os.path.join(temp_build_dir, directory)) for image in images: shutil.copy(get_full_path(image.downsampled_image.path), temp_build_dir) for album in portfolio.albums: create_gallery_webpage(album, portfolio_id, temp_build_dir) primary_album = Album.query.get(portfolio.primary_album_id) shutil.copy( os.path.join(temp_build_dir, '{}.html'.format(primary_album.name)), os.path.join(temp_build_dir, 'index.html')) ## generate the whole website ## Copy in the CSS and JS files original_js_dir = get_full_path('js') js_files = glob.glob('{}/*.js'.format(original_js_dir)) concat_files( js_files, os.path.join(temp_build_dir, 'js', 'nicktardif.min.js')) original_css_dir = get_full_path('css') css_files = glob.glob('{}/*.css'.format(original_css_dir)) concat_files( css_files, os.path.join(temp_build_dir, 'css', 'nicktardif.min.css')) shutil.copy(get_full_path('css/default-skin.svg'), os.path.join(temp_build_dir, 'css')) ## Copy in the favicon file shutil.copy(get_full_path('assets/favicon.ico'), temp_build_dir) shutil.rmtree(app.config['BUILD_DIR']) shutil.move(temp_build_dir, app.config['BUILD_DIR']) return '', status.HTTP_200_OK