plug-ins: port Python plug-in palette-to-gradient to introspected API.

Localization still doesn't work, but this is normal (po-python is not
installed). I will later make the proper tests for this.

Other than this, it is a pretty simple port. It lost all particularities
and facilities of pygimp, but the fact that it now works similarly to
the C API is quite nice too.
It still uses the legacy API for plug-ins though and will have to be
ported further when the new API will be stable.

Also I still haven't figured out why we need to return the number of
returned values. With the proper annotations, an array length parameter
disappears in introspected Python (because it is useless as Python lists
know their length). But it would seem that this annotation doesn't work
the same for returned values, which is a bit sad as it creates ugly
redundancy.

It can be noted that I an going to move all Python plug-ins from
plug-ins/pygimp/plug-ins/ to plug-ins/python/. The whole pygimp/
subdirectory will actually be deleted eventually (I keep it around for
now as reference) as Python plug-in should not need to be considered
particularly from now on. They can just be considered as generic
executables.
This commit is contained in:
Jehan 2019-07-28 18:29:42 +02:00
parent 521ff83cba
commit 65fb7536e1
6 changed files with 178 additions and 152 deletions

View File

@ -2869,6 +2869,7 @@ plug-ins/print/Makefile
dnl plug-ins/pygimp/Makefile
dnl plug-ins/pygimp/plug-ins/Makefile
[
plug-ins/python/Makefile
plug-ins/screenshot/Makefile
plug-ins/script-fu/Makefile
plug-ins/script-fu/ftx/Makefile

View File

@ -14,6 +14,7 @@ endif
if BUILD_PYTHON
pygimp = pygimp
python = python
endif
if OS_WIN32
@ -55,6 +56,7 @@ SUBDIRS = \
metadata \
pagecurl \
$(print) \
$(python) \
screenshot \
selection-to-path \
$(twain) \

View File

@ -1,64 +0,0 @@
## Process this file with automake to produce Makefile.in
pluginexecdir = $(gimpplugindir)/plug-ins
source_scripts = \
colorxhtml.py \
file-openraster.py \
foggify.py \
gradients-save-as-css.py \
histogram-export.py \
palette-offset.py \
palette-sort.py \
palette-to-gradient.py \
py-slice.py \
python-eval.py \
spyro_plus.py \
\
benchmark-foreground-extract.py \
clothify.py \
shadow_bevel.py \
sphere.py \
whirlpinch.py
scripts = \
colorxhtml/colorxhtml.py \
file-openraster/file-openraster.py \
foggify/foggify.py \
gradients-save-as-css/gradients-save-as-css.py \
histogram-export/histogram-export.py \
palette-offset/palette-offset.py \
palette-sort/palette-sort.py \
palette-to-gradient/palette-to-gradient.py \
py-slice/py-slice.py \
python-eval/python-eval.py \
spyro_plus/spyro_plus.py
test_scripts = \
benchmark-foreground-extract/benchmark-foreground-extract.py \
clothify/clothify.py \
shadow_bevel/shadow_bevel.py \
sphere/sphere.py \
whirlpinch/whirlpinch.py
$(scripts) $(test_scripts): $(source_scripts)
$(AM_V_GEN) mkdir -p $(@D) && cp -f "$(srcdir)/$(@F)" $@
nobase_pluginexec_SCRIPTS = $(scripts)
if GIMP_UNSTABLE
nobase_pluginexec_SCRIPTS += $(test_scripts)
endif
# python-console has a data file.
# Therefore let's move it to its own sub-directory.
consoleexecdir = $(gimpplugindir)/plug-ins/python-console
console_scripts = python-console.py
consoleexec_SCRIPTS = $(console_scripts)
dist_consoleexec_DATA = pyconsole.py
EXTRA_DIST = \
$(source_scripts) \
$(console_scripts)
CLEANFILES = $(scripts) $(test_scripts)

View File

@ -1,88 +0,0 @@
#!/usr/bin/env python2
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from gimpfu import *
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def make_gradient(palette, num_segments, num_colors):
gradient = pdb.gimp_gradient_new(palette)
if (num_segments > 1):
pdb.gimp_gradient_segment_range_split_uniform(gradient, 0, -1,
num_segments)
for color_number in range(0,num_segments):
if (color_number == num_colors-1):color_number_next = 0
else: color_number_next = color_number + 1
color_left = pdb.gimp_palette_entry_get_color(palette,
color_number)
color_right = pdb.gimp_palette_entry_get_color(palette,
color_number_next)
pdb.gimp_gradient_segment_set_left_color(gradient,
color_number, color_left,
100.0)
pdb.gimp_gradient_segment_set_right_color(gradient,
color_number, color_right,
100.0)
pdb.gimp_context_set_gradient(gradient)
return gradient
def palette_to_gradient_repeating(palette):
num_colors = pdb.gimp_palette_get_info(palette)
num_segments = num_colors
return make_gradient(palette, num_segments, num_colors)
register(
"python-fu-palette-to-gradient-repeating",
N_("Create a repeating gradient using colors from the palette"),
"Create a new repeating gradient using colors from the palette.",
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
"Carol Spears",
"2006",
N_("Palette to _Repeating Gradient"),
"",
[(PF_PALETTE, "palette", _("Palette"), "")],
[(PF_GRADIENT, "new-gradient", "Result")],
palette_to_gradient_repeating,
menu="<Palettes>",
domain=("gimp20-python", gimp.locale_directory)
)
def palette_to_gradient(palette):
num_colors = pdb.gimp_palette_get_info(palette)
num_segments = num_colors - 1
return make_gradient(palette, num_segments, num_colors)
register(
"python-fu-palette-to-gradient",
N_("Create a gradient using colors from the palette"),
"Create a new gradient using colors from the palette.",
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
"Carol Spears",
"2006",
N_("Palette to _Gradient"),
"",
[(PF_PALETTE, "palette", _("Palette"), "")],
[(PF_GRADIENT, "new-gradient", "Result")],
palette_to_gradient,
menu="<Palettes>",
domain=("gimp20-python", gimp.locale_directory)
)
main ()

View File

@ -0,0 +1,64 @@
## Process this file with automake to produce Makefile.in
pluginexecdir = $(gimpplugindir)/plug-ins
source_scripts = \
## colorxhtml.py \
## file-openraster.py \
## foggify.py \
## gradients-save-as-css.py \
## histogram-export.py \
## palette-offset.py \
## palette-sort.py \
palette-to-gradient.py
## py-slice.py \
## python-eval.py \
## spyro_plus.py \
## \
## benchmark-foreground-extract.py \
## clothify.py \
## shadow_bevel.py \
## sphere.py \
## whirlpinch.py
scripts = \
## colorxhtml/colorxhtml.py \
## file-openraster/file-openraster.py \
## foggify/foggify.py \
## gradients-save-as-css/gradients-save-as-css.py \
## histogram-export/histogram-export.py \
## palette-offset/palette-offset.py \
## palette-sort/palette-sort.py \
palette-to-gradient/palette-to-gradient.py
## py-slice/py-slice.py \
## python-eval/python-eval.py \
## spyro_plus/spyro_plus.py
#test_scripts = \
# benchmark-foreground-extract/benchmark-foreground-extract.py \
# clothify/clothify.py \
# shadow_bevel/shadow_bevel.py \
# sphere/sphere.py \
# whirlpinch/whirlpinch.py
$(scripts) $(test_scripts): $(source_scripts)
$(AM_V_GEN) mkdir -p $(@D) && cp -f "$(srcdir)/$(@F)" $@
nobase_pluginexec_SCRIPTS = $(scripts)
if GIMP_UNSTABLE
nobase_pluginexec_SCRIPTS += $(test_scripts)
endif
# python-console has a data file.
# Therefore let's move it to its own sub-directory.
#consoleexecdir = $(gimpplugindir)/plug-ins/python-console
#console_scripts = python-console.py
#consoleexec_SCRIPTS = $(console_scripts)
#dist_consoleexec_DATA = pyconsole.py
EXTRA_DIST = \
$(source_scripts)
# $(console_scripts)
CLEANFILES = $(scripts) $(test_scripts)

View File

@ -0,0 +1,111 @@
#!/usr/bin/python3
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
import sys
import gettext
_ = gettext.gettext
def N_(message): return message
def make_gradient(palette, num_segments, num_colors):
gradient = Gimp.gradient_new(palette)
if (num_segments > 1):
Gimp.gradient_segment_range_split_uniform(gradient, 0, -1,
num_segments)
for color_number in range(0,num_segments):
if color_number == num_colors - 1:
color_number_next = 0
else:
color_number_next = color_number + 1
_, color_left = Gimp.palette_entry_get_color(palette, color_number)
_, color_right = Gimp.palette_entry_get_color(palette, color_number_next)
Gimp.gradient_segment_set_left_color(gradient,
color_number, color_left,
100.0)
Gimp.gradient_segment_set_right_color(gradient,
color_number, color_right,
100.0)
Gimp.context_set_gradient(gradient)
retval = [Gimp.param_from_status (Gimp.PDBStatusType.SUCCESS),
Gimp.param_from_string(gradient)]
return len(retval), retval
def palette_to_gradient_repeating(palette):
(_, num_colors) = Gimp.palette_get_info(palette)
num_segments = num_colors
return make_gradient(palette, num_segments, num_colors)
def palette_to_gradient(palette):
(_, num_colors) = Gimp.palette_get_info(palette)
num_segments = num_colors - 1
return make_gradient(palette, num_segments, num_colors)
def run(name, n_params, params):
# run_mode = params[0].get_int32()
if name == 'python-fu-palette-to-gradient-repeating':
return palette_to_gradient_repeating(Gimp.context_get_palette())
else:
return palette_to_gradient(Gimp.context_get_palette())
def query():
param = Gimp.ParamDef()
param.type = Gimp.PDBArgType.INT32
param.name = "run-mode"
param.description = _("Run mode")
retval = Gimp.ParamDef()
retval.type = Gimp.PDBArgType.STRING
retval.name = "new-gradient"
retval.description = _("Result")
Gimp.install_procedure(
"python-fu-palette-to-gradient-repeating",
N_("Create a repeating gradient using colors from the palette"),
"Create a new repeating gradient using colors from the palette.",
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
"Carol Spears",
"2006",
N_("Palette to _Repeating Gradient"),
"",
Gimp.PDBProcType.PLUGIN,
[ param ],
[ retval ])
Gimp.plugin_menu_register("python-fu-palette-to-gradient-repeating", "<Palettes>")
Gimp.install_procedure(
"python-fu-palette-to-gradient",
N_("Create a gradient using colors from the palette"),
"Create a new gradient using colors from the palette.",
"Carol Spears, reproduced from previous work by Adrian Likins and Jeff Trefftz",
"Carol Spears",
"2006",
N_("Palette to _Gradient"),
"",
Gimp.PDBProcType.PLUGIN,
[ param ],
[ retval ])
Gimp.plugin_menu_register("python-fu-palette-to-gradient", "<Palettes>")
#Gimp.plugin_domain_register("gimp20-python", Gimp.locale_directory())
#gettext.install("gimp20-python", localedir=Gimp.locale_directory(), codeset='utf-8')
info = Gimp.PlugInInfo ()
info.set_callbacks (None, None, query, run)
Gimp.main_legacy (info, sys.argv)