Last active
October 27, 2020 17:12
-
-
Save ammancilla/cf9dd21d0193c374a442dd0392e29d98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Simple python module to add a black fadeout effect to a given image | |
| # | |
| # | |
| # apt-get install -y gimp-python | |
| # | |
| # gimp -i -d -f --batch-interpreter python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import gimp_fadeout; gimp_fadeout.apply()' | |
| # | |
| import os | |
| from gimpfu import * | |
| def apply(): | |
| image_path = os.environ['GIMP_FADEOUT_IMAGE'] | |
| if not os.path.exists(image_path): | |
| raise NameError('image does not exists') | |
| image_filename = os.path.basename(image_path) | |
| black_image_path = 'black_image_' + image_filename | |
| fadeout_path = 'fadeout_' + image_filename | |
| # --- | |
| # Context Configuration | |
| # --- | |
| print('Configuring Gimp context...') | |
| pdb.gimp_context_set_default_colors() | |
| pdb.gimp_context_set_gradient_fg_bg_rgb() | |
| # --- | |
| # Create base black image | |
| # --- | |
| print('Creating base black image...') | |
| image = pdb.gimp_file_load(image_path, RUN_NONINTERACTIVE) | |
| width = image.width | |
| height = image.height | |
| layer = gimp.Layer(image, 'Black Layer', width, height, RGB_IMAGE, 100, NORMAL_MODE) | |
| image.add_layer(layer) | |
| pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE) | |
| pdb.file_jpeg_save(image, image.active_layer, black_image_path, black_image_path, 1,0,1,0,0,0,0,0,0) | |
| pdb.gimp_image_delete(image) | |
| # --- | |
| # Create Fadeout | |
| # --- | |
| print('Adding fadeout to original image...') | |
| # Open black image | |
| black_image = pdb.gimp_file_load(black_image_path, RUN_NONINTERACTIVE) | |
| # Load main image as layer | |
| image = pdb.gimp_file_load_layer(black_image, image_path) | |
| black_image.add_layer(image) | |
| # Create layer mask | |
| mask = pdb.gimp_layer_create_mask(image, ADD_MASK_WHITE) | |
| # Add layer mas to main image | |
| pdb.gimp_layer_add_mask(image, mask) | |
| # Apply gradient | |
| x1 = width / 2 | |
| y1 = height * 0.98 | |
| y2 = y1 - (height * 0.1) | |
| pdb.gimp_drawable_edit_gradient_fill(mask, GRADIENT_LINEAR, 0, 0, 0, 0, 0, x1, y1, x1, y2) | |
| # Merge layers into black image | |
| pdb.gimp_image_merge_visible_layers(black_image, CLIP_TO_IMAGE) | |
| # --- | |
| # Save Image with Fadeout | |
| # --- | |
| print('Saving image with fadeout...') | |
| pdb.file_jpeg_save(black_image, black_image.active_layer, fadeout_path, fadeout_path, 1,0,1,0,0,0,0,0,0) | |
| # --- | |
| # Clean Up | |
| # --- | |
| print('Done!!!') | |
| # Delete temporarily files | |
| os.remove(black_image_path) | |
| # Delete images from gimp context | |
| pdb.gimp_image_delete(black_image) | |
| # Close gimp | |
| pdb.gimp_quit(1) | |
| # ----- | |
| # FADEOUT TECNIQUE 2 | |
| # ---- | |
| # def fadeout(infile, percentile = 0.9): | |
| # # 5. Get the image - Load the image | |
| # infile = "/home/oferfacil/bron.jpg" | |
| # outfile = "/home/oferfacil/fadeout.jpg" | |
| # image = pdb.gimp_file_load(infile, RUN_NONINTERACTIVE) | |
| # width = image.width | |
| # height = image.height | |
| # # 2. Create a layer | |
| # layer = gimp.Layer(image, 'Overlay', width, height, RGB_IMAGE, 100, NORMAL_MODE) | |
| # # 3. Add layer to image | |
| # image.add_layer(layer) | |
| # # 4. Create mask | |
| # mask = pdb.gimp_layer_create_mask(layer, ADD_MASK_WHITE) | |
| # # 5. Add mask to layer | |
| # pdb.gimp_layer_add_mask(layer, mask) | |
| # # 6. Apply Gradient - fadeout | |
| # cx = width / 2 | |
| # cy = height * percentile | |
| # pdb.gimp_context_set_default_colors() | |
| # pdb.gimp_context_set_gradient_fg_bg_hsv_ccw() | |
| # pdb.gimp_drawable_edit_gradient_fill(mask, GRADIENT_BILINEAR, 0, 0, 0, 0, 0, cx, 0, cx, cy) | |
| # # 7. Merge layers | |
| # pdb.gimp_image_merge_visible_layers(image, CLIP_TO_IMAGE) | |
| # # 7. Save image with fadeout | |
| # drawable = image.active_layer | |
| # pdb.file_jpeg_save(image, drawable, outfile, outfile, 1,0,1,0,0,0,0,0,0) | |
| # # pdb.file_png_save(image, drawable, outfile, outfile, 0,9,0,0,0,0,0) | |
| # # 9. Delete original from gimp context | |
| # pdb.gimp_image_delete(image) | |
| # # 10. Quit gimp | |
| # pdb.gimp_quit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment