-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhsv_layers.py
executable file
·308 lines (206 loc) · 8.26 KB
/
hsv_layers.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# -*- coding: utf8 -*-
# ***************************************************************************
#
# Component Layers
#
# $Id: hsv_layers.py,v 1.71 2014/02/08 21:32:31 sjg Exp $
#
# (c) Stephen Geary, Apr 2011
#
# Split an layer into separate color component Layers in different spaces
#
# ***************************************************************************
from gimpfu import *
import math,random
from array import array
# ***************************************************************************
def plugin_spaces( idx ):
if not hasattr( plugin_spaces, "colorspaces" ):
# initialize
plugin_spaces.colorspaces = [ "RGB", "HSV", "LAB", "HSL", "CMY" ]
if idx == -1:
return plugin_spaces.colorspaces
else:
return plugin_spaces.colorspaces[idx]
# ***************************************************************************
def sjg_component_layers( image, drawable, spaceidx ):
colorspace = plugin_spaces( spaceidx )
pdb.gimp_image_undo_group_start(image)
im = pdb.plug_in_decompose( image, drawable, colorspace, 0 )
l0 = pdb.gimp_layer_new_from_visible( im[0], image, colorspace[0] + " component" )
l1 = pdb.gimp_layer_new_from_visible( im[1], image, colorspace[1] + " component" )
l2 = pdb.gimp_layer_new_from_visible( im[2], image, colorspace[2] + " component" )
image.add_layer( l0, -1 )
image.add_layer( l1, -1 )
image.add_layer( l2, -1 )
pdb.gimp_image_undo_group_end(image)
gimp.displays_flush()
# ***************************************************************************
def makeMask( image, drawable, optype, pos ):
if image.active_layer.mask != None:
gimp.message( "Layer already has a mask. Cannot proceed." )
return
# Basically this is a decompose op, creating a mask for the
# decompose image that represents the component we want
# and a copy and paste of that mask to the active layer.
pdb.gimp_image_undo_group_start(image)
im = pdb.plug_in_decompose( image, drawable, optype, 0 )
msk = pdb.gimp_layer_create_mask( im[pos].layers[0], ADD_COPY_MASK )
pdb.gimp_layer_add_mask( im[pos].layers[0], msk )
pdb.gimp_edit_copy( msk )
newmsk = pdb.gimp_layer_create_mask( image.active_layer, ADD_COPY_MASK )
pdb.gimp_layer_add_mask( image.active_layer, newmsk )
flt = pdb.gimp_edit_paste( newmsk, 0 )
pdb.gimp_floating_sel_anchor( flt )
# clean up the non-displayed temp images we made
for img in im:
# Note that "None" actually does appear in the list
# so we do need to check
if img != None:
pdb.gimp_image_delete( img )
pdb.gimp_image_undo_group_end( image )
gimp.displays_flush()
def quickmask_R_RGB( image, drawable ):
makeMask( image, drawable, "RGB", 0 )
def quickmask_G_RGB( image, drawable ):
makeMask( image, drawable, "RGB", 1 )
def quickmask_B_RGB( image, drawable ):
makeMask( image, drawable, "RGB", 2 )
def quickmask_H_HSV( image, drawable ):
makeMask( image, drawable, "HSV", 0 )
def quickmask_S_HSV( image, drawable ):
makeMask( image, drawable, "HSV", 1 )
def quickmask_V_HSV( image, drawable ):
makeMask( image, drawable, "HSV", 2 )
def quickmask_L_LAB( image, drawable ):
makeMask( image, drawable, "LAB", 0 )
def quickmask_A_LAB( image, drawable ):
makeMask( image, drawable, "LAB", 1 )
def quickmask_B_LAB( image, drawable ):
makeMask( image, drawable, "LAB", 2 )
def quickmask_C_CMY( image, drawable ):
makeMask( image, drawable, "CMY", 0 )
def quickmask_Y_CMY( image, drawable ):
makeMask( image, drawable, "CMY", 1 )
def quickmask_M_CMY( image, drawable ):
makeMask( image, drawable, "CMY", 2 )
def quickmask_H_HSL( image, drawable ):
makeMask( image, drawable, "HSL", 0 )
def quickmask_S_HSL( image, drawable ):
makeMask( image, drawable, "HSL", 1 )
def quickmask_L_HSL( image, drawable ):
makeMask( image, drawable, "HSL", 2 )
def quickmask_C_CMYK( image, drawable ):
makeMask( image, drawable, "CMYK", 0 )
def quickmask_M_CMYK( image, drawable ):
makeMask( image, drawable, "CMYK", 1 )
def quickmask_Y_CMYK( image, drawable ):
makeMask( image, drawable, "CMYK", 2 )
def quickmask_K_CMYK( image, drawable ):
makeMask( image, drawable, "CMYK", 3 )
def quickmask_Y_YCbCr_ITU_R470( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R470", 0 )
def quickmask_Cb_YCbCr_ITU_R470( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R470", 1 )
def quickmask_Cr_YCbCr_ITU_R470( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R470", 2 )
def quickmask_Y_YCbCr_ITU_R709( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R709", 0 )
def quickmask_Cb_YCbCr_ITU_R709( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R709", 1 )
def quickmask_Cr_YCbCr_ITU_R709( image, drawable ):
makeMask( image, drawable, "YCbCr_ITU_R709", 2 )
def quickmask_Y_YCbCr_ITU_R470_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R470 256", 0 )
def quickmask_Cb_YCbCr_ITU_R470_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R470 256", 1 )
def quickmask_Cr_YCbCr_ITU_R470_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R470 256", 2 )
def quickmask_Y_YCbCr_ITU_R709_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R709 256", 0 )
def quickmask_Cb_YCbCr_ITU_R709_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R709 256", 1 )
def quickmask_Cr_YCbCr_ITU_R709_256( image, drawable ):
makeMask( image, drawable, "YCbCr ITU R709 256", 2 )
# ***************************************************************************
def quickmask_register( shortname ):
i = shortname.find( "_" )
cn = shortname[:i]
sn = shortname[(i+1):]
# GIMP treats menu entries with underlines as accelator key markings
# and will replace the underscore and next char with an underlined version
# of that character. So we replace the underscores with spaces.
sn = sn.replace( "_", " " )
desc = "Create a layer mask from the " + cn + " component of the " + sn + " representation of the image."
# For the register call we need to construct some names
# for the GIMP PDB, the menu entry and the function itself.
#
# The function is found by looking up globals() with the
# name of the function required.
#
# GIMP's UI reorders sub-menu entries in alphabetical order,
# which means we need to be careful about menu entries so
# that the color spaces are close together in the menu.
register(
"sjg_quickmask_" + cn + "_from_" + shortname[(i+1):],
desc,
desc,
"Stephen Geary",
"(©) 2014 Stephen Geary",
"2014-02-07",
"<Image>/Layer/Quick Mask/" + sn + " channel " + cn ,
"RGB*",
[
],
[],
globals()[ "quickmask_" + shortname ],
)
quickmask_register( "R_RGB" )
quickmask_register( "G_RGB" )
quickmask_register( "B_RGB" )
quickmask_register( "H_HSV" )
quickmask_register( "S_HSV" )
quickmask_register( "V_HSV" )
quickmask_register( "L_LAB" )
quickmask_register( "A_LAB" )
quickmask_register( "B_LAB" )
quickmask_register( "C_CMY" )
quickmask_register( "Y_CMY" )
quickmask_register( "M_CMY" )
quickmask_register( "H_HSL" )
quickmask_register( "S_HSL" )
quickmask_register( "L_HSL" )
quickmask_register( "C_CMYK" )
quickmask_register( "Y_CMYK" )
quickmask_register( "M_CMYK" )
quickmask_register( "M_CMYK" )
quickmask_register( "Y_YCbCr_ITU_R470" )
quickmask_register( "Cb_YCbCr_ITU_R470" )
quickmask_register( "Cr_YCbCr_ITU_R470" )
quickmask_register( "Y_YCbCr_ITU_R709" )
quickmask_register( "Cb_YCbCr_ITU_R709" )
quickmask_register( "Cr_YCbCr_ITU_R709" )
quickmask_register( "Y_YCbCr_ITU_R470_256" )
quickmask_register( "Cb_YCbCr_ITU_R470_256" )
quickmask_register( "Cr_YCbCr_ITU_R470_256" )
quickmask_register( "Y_YCbCr_ITU_R709_256" )
quickmask_register( "Cb_YCbCr_ITU_R709_256" )
quickmask_register( "Cr_YCbCr_ITU_R709_256" )
register(
"sjg_component_layers_fn",
"Split a layer into different color space component layers",
"Split a layer into different color space component layers",
"Stephen Geary",
"(©) 2011 Stephen Geary",
"2011-04-23",
"<Image>/Colors/Components/Color Component Layers",
"RGB*",
[
( PF_OPTION, "spaceidx", "Component Space", 0, plugin_spaces(-1) )
],
[],
sjg_component_layers,
)
main()
# ***************************************************************************