Last active
October 27, 2020 17:12
-
-
Save ammancilla/cf9dd21d0193c374a442dd0392e29d98 to your computer and use it in GitHub Desktop.
This file contains 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 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 sys, os | |
from gimpfu import * | |
def apply(): | |
try: | |
# --- | |
# Configuration | |
# --- | |
fadeout_from = 0.98 | |
fadeout_until = 0.1 | |
image_path = os.environ['FADEOUT_IMAGE'] | |
if not os.path.exists(image_path): | |
raise NameError('image does not exists') | |
if 'FADEOUT_FROM' in os.environ and os.environ['FADEOUT_FROM']: | |
fadeout_from = float(os.environ['FADEOUT_FROM']) | |
if 'FADEOUT_UNTIL' in os.environ and os.environ['FADEOUT_UNTIL']: | |
fadeout_until = float(os.environ['FADEOUT_UNTIL']) | |
image_filename = os.path.basename(image_path) | |
black_image_path = 'black_image_' + image_filename | |
fadeout_path = 'fadeout_' + image_filename | |
# --- | |
# Gimp 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 * fadeout_from | |
y2 = y1 - (y1 * fadeout_until) | |
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) | |
except Exception as e: | |
print("ERROR: " + str(e)) | |
finally: | |
pdb.gimp_quit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment