-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw_outline.py
More file actions
167 lines (129 loc) · 4.73 KB
/
draw_outline.py
File metadata and controls
167 lines (129 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import gi
gi.require_version("Gimp", "3.0")
from gi.repository import Gimp
THICKNESS = 3
DEFAULT_LAYER_MODE = Gimp.LayerMode.NORMAL
DEFAULT_FILL_TYPE = Gimp.FillType.BACKGROUND
class DrawOutline(Gimp.PlugIn):
__gtype_name__ = "PythonDrawOutline"
def do_query_procedures(self):
return ["python-fu-draw-outline"]
def do_create_procedure(self, name):
procedure = Gimp.ImageProcedure.new(
self,
name,
Gimp.PDBProcType.PLUGIN,
self.draw_outline,
None,
)
procedure.set_documentation(
"Draw outline",
"Expands the active drawable and paints the selection with the background color to mimic an outline.",
"yukihane",
)
procedure.set_attribution("yukihane", "yukihane", "2024")
procedure.set_menu_label("Draw outline")
procedure.add_menu_path("<Image>/Filters/Languages/Python-Fu")
procedure.set_image_types("RGB*, GRAY*")
procedure.set_sensitivity_mask(
Gimp.ProcedureSensitivityMask.DRAWABLE |
Gimp.ProcedureSensitivityMask.DRAWABLES
)
return procedure
def draw_outline(
self,
procedure,
run_mode,
image,
drawables,
config,
run_data,
):
pdb = Gimp.get_pdb()
target = self._resolve_target_layer(drawables, pdb, image)
if target is None:
return procedure.new_return_values(Gimp.PDBStatusType.CANCEL, None)
thickness = THICKNESS
try:
position = image.get_item_position(target)
except AttributeError:
position = -1
offset_x, offset_y = self._get_layer_offsets(target)
width = target.get_width() + (thickness * 2)
height = target.get_height() + (thickness * 2)
Gimp.Selection.none(image)
Gimp.context_set_antialias(False)
Gimp.context_set_feather(False)
if not self._select_alpha(image, target, pdb):
return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, None)
Gimp.Selection.grow(image, thickness)
outline_layer = Gimp.Layer.new(
image,
f"ol_{target.get_name()}",
width,
height,
Gimp.ImageType.RGBA_IMAGE,
100.0,
DEFAULT_LAYER_MODE,
)
outline_layer.set_offsets(offset_x - thickness, offset_y - thickness)
parent = target.get_parent() if hasattr(target, "get_parent") else None
insertion_index = position + 1 if position >= 0 else -1
image.insert_layer(outline_layer, parent, insertion_index)
self._fill_drawable(outline_layer, DEFAULT_FILL_TYPE)
self._apply_selection_mask(pdb, outline_layer)
Gimp.Selection.none(image)
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, None)
@staticmethod
def _resolve_target_layer(drawables, pdb, image):
if drawables:
if isinstance(drawables, (list, tuple)):
for drawable in drawables:
if drawable is not None:
return drawable
else:
return drawables
selected = pdb.gimp_image_get_selected_drawables(image)
if isinstance(selected, tuple) and len(selected) == 2:
primary, secondary = selected
if isinstance(primary, (list, tuple)):
for drawable in primary:
if drawable is not None:
return drawable
if secondary is not None:
return secondary
elif selected:
return selected
return None
@staticmethod
def _get_layer_offsets(layer):
offsets = layer.get_offsets()
parsed = DrawOutline._parse_offsets(offsets)
if parsed is not None:
return parsed
return 0, 0
@staticmethod
def _fill_drawable(drawable, fill_type):
drawable.fill(fill_type)
@staticmethod
def _apply_selection_mask(pdb, layer):
mask = layer.create_mask(Gimp.AddMaskType.SELECTION)
layer.add_mask(mask)
layer.remove_mask(Gimp.MaskApplyMode.APPLY)
@staticmethod
def _select_alpha(image, drawable, pdb):
image.select_item(Gimp.ChannelOps.REPLACE, drawable)
return True
@staticmethod
def _parse_offsets(offsets):
if not isinstance(offsets, tuple):
return None
if len(offsets) >= 3 and isinstance(offsets[0], bool):
return offsets[1], offsets[2]
if len(offsets) >= 2:
return offsets[0], offsets[1]
return None
Gimp.main(DrawOutline.__gtype__, sys.argv)