Here's the code this revision (3).
#!/usr/bin/env python
# bibo-text.py
# Created by TT
# This plug-in will accept a background layer, center-x, center-y and a sentence/phrase with multiple words separated by spaces.
# It will then animate by resizing the first word smaller and smaller then switches and resizes 2nd words larger and larger like as if you're breathing in and out.
#
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release
# Rel 2: MareroQ on http://gimpchat.com/viewtopic.php?f=9&t=20604&start=10#p284795 prefers the word change when the text is small or when we just finished exhaling
# so I added a "word per breath" toggle
# Rel 3: MareroQ on http://gimpchat.com/viewtopic.php?f=9&t=20604&p=284804#p284804 recommended ability to change start and end opacity
# so I added these 2 input params
# And ability to choose Use Image Center toggle
from gimpfu import *
import random
def ease_in_out(t):
"""Ease-in-out function: acceleration until halfway, then deceleration"""
return 2*t*t if t < 0.5 else 1 - pow(-2*t + 2, 2) / 2
def python_bibo_text(image, layer, cx,cy,phrase,minsize,maxsize,framesin,framesex,wordpb,opastart,opaend,useimagecenter):
# pdb.gimp_image_undo_group_start(image)
# pdb.gimp_context_push()
#YOUR CODE BEGINS=======================
if (int(useimagecenter)==1):
cx = image.width/2
cy = image.height/2
opastart = opastart/100.0
opaend = opaend/100.0
wordpb = int(wordpb)
framesin = int(framesin)
framesex = int(framesex)
words = phrase.split(" ")
exhale = 1
if (wordpb == 0):
loop = len(words)
else:
loop = len(words)*2 #if word per breath we do twice the number of loop
fontname = pdb.gimp_context_get_font()
new_image = pdb.gimp_image_new(image.width,image.height,RGB)
pdb.gimp_display_new(new_image)
for i in range(0,loop):
if (wordpb == 0):
word = words[i]
else:
word = words[int(i/2)];
if exhale == 0:
frames = framesin
else:
frames = framesex
for j in range(0,frames):
if exhale == 0:
t = 1.0-float(j)/frames
else:
t = 0.0+float(j)/frames
size = minsize + (maxsize-minsize) * ease_in_out(t)
opacity = opaend + (opastart-opaend) * ease_in_out(t)
text_layer = pdb.gimp_text_fontname(image,None,0,0,word,-1,TRUE,size,0,fontname)
pdb.gimp_layer_set_offsets(text_layer,cx-text_layer.width/2,cy-text_layer.height/2)
pdb.gimp_layer_set_opacity(text_layer,opacity * 100)
new_layer = pdb.gimp_layer_new_from_visible(image,new_image,'frame')
pdb.gimp_image_insert_layer(new_image,new_layer,None,0)
pdb.gimp_image_remove_layer(image,text_layer)
exhale = abs(exhale-1) #toggle between 0 and 1
#YOUR CODE ENDS ========================
# pdb.gimp_context_pop()
# pdb.gimp_image_undo_group_end(image)
pdb.gimp_displays_flush()
#return
register(
"python_fu_bibo_text",
"Creates Bibo-text",
"Creates Bibo-text",
"TT",
"TT",
"October 26, 2023",
"<Image>/Python-Fu/BIBO-text...",
"*", # Create a new image, don't work on an existing one
[
#INPUT BEGINS
(PF_INT, "cx", "Center-X:", 452),
(PF_INT, "cy", "Center-Y:", 210),
(PF_TEXT, "phrase", "Text Phrase:", "NAM MO A DI DA PHAT"),
(PF_INT, "minsize", "Min Size:", 50),
(PF_INT, "maxsize", "Max Size:", 150),
(PF_INT, "framesin", "Frames Per Inhaling:", 15), #1.5 to 2 seconds
(PF_INT, "framesout", "Frames Per Exhaling:", 20), #2 to 3 seconds.
(PF_TOGGLE, "wordpb", "Word Per Breath:", 0), # initially True, checked. Alias PF_BOOL
(PF_INT, "opastart", "Opacity Start:", 0),
(PF_INT, "opaend", "Opacity End:", 100),
(PF_TOGGLE, "useimagecenter", "Use Image Center", 0), # initially True, checked. Alias PF_BOOL
#INPUT ENDS
],
[],
python_bibo_text)
main()
# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste
# (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32 similar but no difference in Python.
# (PF_FLOAT, "p02", "_FLOAT:", 3.141),
# (PF_STRING, "p03", "_STRING:", "foo"), # alias PF_VALUE
# (PF_TEXT, "p04", "TEXT:", "bar"),
# # PF_VALUE
# # Pick one from set of choices
# (PF_OPTION,"p1", "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
# (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
# # PF_RADIO is usually called a radio button group.
# # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
# (PF_TOGGLE, "p2", "TOGGLE:", 1), # initially True, checked. Alias PF_BOOL
# # PF_TOGGLE is usually called a checkbox.
# (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
# (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)), # alias PF_ADJUSTMENT
# # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
# (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
# # PF_COLOUR is an alias by aussie PyGimp author lol
# (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
# (PF_FONT, "p17", "FONT:", 0),
# (PF_FILE, "p18", "FILE:", 0),
# (PF_BRUSH, "p19", "BRUSH:", 0),
# (PF_PATTERN, "p20", "PATTERN:", 0),
# (PF_GRADIENT, "p21", "GRADIENT:", 0),
# (PF_PALETTE, "p22", "PALETTE:", 0),
# (PF_LAYER, "p23", "LAYER:", None),
# (PF_CHANNEL, "p24", "CHANNEL:", None), # ??? Usually empty, I don't know why.
# (PF_DRAWABLE, "p25", "DRAWABLE:", None),
# # Mostly undocumented, but work
# (PF_VECTORS, "p26", "VECTORS:", None),
# (PF_FILENAME, "p27", "FILENAME:", 0),
# (PF_DIRNAME, "p28", "DIRNAME:", 0)
# # PF_REGION might work but probably of little use. See gimpfu.py.