Skip to content

Instantly share code, notes, and snippets.

@ammancilla
Last active October 27, 2020 17:12
Show Gist options
  • Select an option

  • Save ammancilla/cf9dd21d0193c374a442dd0392e29d98 to your computer and use it in GitHub Desktop.

Select an option

Save ammancilla/cf9dd21d0193c374a442dd0392e29d98 to your computer and use it in GitHub Desktop.
#
# Simple python module to add a black fadeout effect to a given image
#
import os
from gimpfu import *
def gimp_fadeout():
image_path = os.environ['IMAGE']
# image_path = '/Users/drilococo/Desktop/bron.jpg'
if not os.path.exists(image_path):
raise NameError('image does not exists')
image_filename = os.path.basename(image_path)
black_image_filename = 'black_image_' + image_filename
black_image_path = '/tmp/' + black_image_filename
fadeout_filename = 'fadeout_' + image_filename
fadeout_path = '/tmp/' + fadeout_filename
# ---
# Context Configuration
# ---
pdb.gimp_context_set_default_colors()
pdb.gimp_context_set_gradient_fg_bg_rgb()
# ---
# Create 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
# ---
# 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 as jpg
pdb.file_jpeg_save(black_image, black_image.active_layer, fadeout_path, fadeout_path, 1,0,1,0,0,0,0,0,0)
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment