File: layerfx_innerlight.py

package info (click to toggle)
lazpaint 7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,000 kB
  • sloc: pascal: 277,538; python: 2,494; makefile: 233; sh: 221
file content (274 lines) | stat: -rw-r--r-- 9,003 bytes parent folder | download
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
# Layer effect > Inner light
# (fr) Effet de calque > Lumière intérieure
from lazpaint import dialog

try:
    from tkinter import Tk, Frame, Label, Scale, HORIZONTAL, Button, RIGHT, W, E
except ImportError:
    dialog.show_message("Please install tkinter. On Debian distributions, use the command: apt install python3-tk")
    exit()
        
from lazpaint import colors, image, layer, filters, tools, selection
import math

translation = dialog.translate_dict(["Layer is empty", "Radius", "Angle", 
    "Opacity", "Ok", "Cancel", "Run this script again to update effect"])

if layer.is_empty():
    dialog.show_message(translation["Layer is empty"])
    exit()

############ image processing

FRIENDLY_NAME = dialog.get_script_name()
REGISTRY_NAME = "innerlight"
OPPOSITE_REGISTRY_NAME = "innershadow"
DEFAULT_ANGLE = 315
DEFAULT_COLOR = colors.WHITE

MAX_RADIUS = 200
MAX_OPACITY = 255
MAX_ANGLE = 360

source_layer_id = layer.get_registry(REGISTRY_NAME+"-source-layer-id")
if source_layer_id is not None:
  layer.select_id(source_layer_id)
else:
  source_layer_id = layer.get_id()
source_layer_name = layer.get_name()

chosen_radius = layer.get_registry(REGISTRY_NAME+"-radius")
if chosen_radius == None: 
    chosen_radius = layer.get_registry(OPPOSITE_REGISTRY_NAME+"-radius")
if chosen_radius == None: 
    chosen_radius = image.get_registry(REGISTRY_NAME+"-radius")
if chosen_radius == None: 
    chosen_radius = image.get_registry(OPPOSITE_REGISTRY_NAME+"-radius")
if chosen_radius == None:
    chosen_radius = 20

chosen_angle = layer.get_registry(REGISTRY_NAME+"-angle")
if chosen_angle == None: 
    chosen_angle = layer.get_registry(OPPOSITE_REGISTRY_NAME+"-angle")
    if chosen_angle is not None:
        chosen_angle = (chosen_angle+180) % 360
if chosen_angle == None: 
    chosen_angle = image.get_registry(REGISTRY_NAME+"-angle")
if chosen_angle == None: 
    chosen_angle = image.get_registry(OPPOSITE_REGISTRY_NAME+"-angle")
    if chosen_angle is not None:
        chosen_angle = (chosen_angle+180) % 360
if chosen_angle == None:
    chosen_angle = DEFAULT_ANGLE

shadow_layer_id = layer.get_registry(REGISTRY_NAME+"-layer-id")
if image.get_layer_index(shadow_layer_id) == None:
    shadow_layer_id = None

if shadow_layer_id is not None:
    layer.select_id(shadow_layer_id)
    chosen_opacity = layer.get_opacity()
    overlay_color = colors.str_to_RGBA(layer.get_registry("overlay-color"))
    layer.select_id(source_layer_id)
else:
    chosen_opacity = layer.get_opacity()*2/3 
    overlay_color = None

if overlay_color is None:
  overlay_color = DEFAULT_COLOR

def create_shadow_layer():
    global shadow_layer_id
    image.do_begin()
    if shadow_layer_id != None:
        layer.select_id(shadow_layer_id)
        layer.remove()
    layer.select_id(source_layer_id)
    layer.duplicate()
    layer.rasterize()
    layer.set_name(FRIENDLY_NAME+" - "+source_layer_name)
    layer.set_registry(REGISTRY_NAME+"-source-layer-id", source_layer_id)
    shadow_layer_id = layer.get_id()
    layer.set_registry("overlay-color", overlay_color)
    layer.set_opacity(chosen_opacity)    
    image.do_end()

blur_done = False
opacity_done = False

def apply_blur():
    global blur_done, opacity_done
    if opacity_done:
        image.undo()
        opacity_done = False
    if blur_done:
        image.undo()
        blur_done = False
    image.do_begin()
    if chosen_radius == 0:
        filters.filter_function(alpha=0, gamma_correction=False)
    else:
        layer.duplicate()
        layer.set_opacity(255)
        filters.filter_function(red="alpha", green="alpha", blue="alpha", alpha=1, gamma_correction=False)
        mask_layer_id = layer.get_id()    

        layer.select_id(shadow_layer_id)
        filters.filter_function(red=overlay_color.red/255, green=overlay_color.green/255, blue=overlay_color.blue/255, gamma_correction=False)
        tools.choose(tools.MOVE_LAYER)
        offset = (math.sin(chosen_angle*math.pi/180)*chosen_radius, -math.cos(chosen_angle*math.pi/180)*chosen_radius)
        tools.mouse([(0,0), (offset[0],offset[1])])

        layer.select_id(mask_layer_id)
        layer.duplicate()
        mask_layer_id2 = layer.get_id()
        layer.select_id(mask_layer_id)
        tools.choose(tools.MOVE_LAYER)
        tools.mouse([(offset[0]/2,offset[1]/2), (0,0)])
        colors.linear_negative()
        layer.set_blend_op(layer.BLEND_MASK)
        layer.merge_over()
        mask_layer_id = mask_layer_id2

        filters.blur(radius=chosen_radius)

        layer.select_id(mask_layer_id)
        layer.set_blend_op(layer.BLEND_MASK)
        layer.merge_over()  
   
    blur_done = image.do_end()
    apply_opacity()

def apply_opacity():
    global opacity_done
    if opacity_done:
        image.undo()
        opacity_done = False
    image.do_begin()
    layer.set_opacity(chosen_opacity)
    opacity_done = image.do_end()

######## interface

def button_ok_click():
    global source_layer_id, chosen_radius, chosen_angle
    layer.select_id(source_layer_id)
    layer.set_registry(REGISTRY_NAME+"-radius", chosen_radius)
    layer.set_registry(REGISTRY_NAME+"-angle", chosen_angle)
    layer.set_registry(REGISTRY_NAME+"-layer-id", shadow_layer_id)
    image.set_registry(REGISTRY_NAME+"-radius", chosen_radius)
    image.set_registry(REGISTRY_NAME+"-angle", chosen_angle)
    image.do_end()
    exit()

def button_cancel_click():    
    if image.do_end():
        image.undo()
    layer.select_id(source_layer_id)
    exit()

scale_radius_update_job = None

def scale_radius_update_do():
    global scale_radius_update_job, chosen_radius, scale_radius
    new_radius = scale_radius.get() 
    if new_radius != chosen_radius:
        chosen_radius = new_radius
        apply_blur()
    scale_radius_update_job = None    

def scale_radius_update(event):
    global window, scale_radius_update_job
    if scale_radius_update_job:
        window.after_cancel(scale_radius_update_job)
    scale_radius_update_job = window.after(800, scale_radius_update_do)

scale_angle_update_job = None

def scale_angle_update_do():
    global scale_angle_update_job, chosen_angle, scale_angle
    new_angle = scale_angle.get() 
    if new_angle != chosen_angle:
        chosen_angle = new_angle
        apply_blur()
    scale_angle_update_job = None    

def scale_angle_update(event):
    global window, scale_angle_update_job
    if scale_angle_update_job:
        window.after_cancel(scale_angle_update_job)
    scale_angle_update_job = window.after(800, scale_angle_update_do)

scale_opacity_update_job = None

def scale_opacity_update_do():
    global chosen_opacity 
    new_opacity = scale_opacity.get()
    if new_opacity != chosen_opacity:
        chosen_opacity = new_opacity
        apply_opacity()
    scale_opacity_update_job = None

def scale_opacity_update(event):   
    global window, scale_opacity_update_job
    if scale_opacity_update_job:
        window.after_cancel(scale_opacity_update_job)
    scale_opacity_update_job = window.after(100, scale_opacity_update_do)

window = Tk()
window.title(FRIENDLY_NAME)
window.resizable(False, False)

frame = Frame(window)
frame.pack()

label_radius = Label(frame, text=translation["Radius"])
label_radius.grid(column=0, row=0)
scale_radius = Scale(frame, from_=0, to=MAX_RADIUS, orient=HORIZONTAL, command=scale_radius_update)
scale_radius.grid(column=1, row=0, sticky=W+E, padx=10)
scale_radius.set(chosen_radius)

label_angle = Label(frame, text=translation["Angle"])
label_angle.grid(column=0, row=1)
scale_angle = Scale(frame, from_=0, to=MAX_ANGLE, orient=HORIZONTAL, command=scale_angle_update)
scale_angle.grid(column=1, row=1, sticky=W+E, padx=10)
scale_angle.set(chosen_angle)

label_opacity = Label(frame, text=translation["Opacity"])
label_opacity.grid(column=0, row=2)
scale_opacity = Scale(frame, from_=0, to=MAX_OPACITY, orient=HORIZONTAL, command=scale_opacity_update)
scale_opacity.grid(column=1, row=2, sticky=W+E, padx=10)
scale_opacity.set(chosen_opacity)

label_info = Label(frame, text=translation["Run this script again to update effect"],
    background="lemon chiffon", foreground="black", borderwidth=1, relief="solid")
label_info.grid(column=0, rows=3, columnspan=2)


frame.columnconfigure(0, pad=20)
frame.columnconfigure(1, minsize=250)
frame.rowconfigure(0, pad=20)
frame.rowconfigure(1, pad=20)
frame.rowconfigure(2, pad=20)

button_ok = Button(window, text=translation["Ok"], command=button_ok_click)
button_ok.pack(side=RIGHT, padx=10, pady=10)
button_cancel = Button(window, text=translation["Cancel"], command=button_cancel_click)
button_cancel.pack(side=RIGHT, pady=10)

image.do_begin()
selection.deselect()
create_shadow_layer()
apply_blur()

window.update()
window_width = window.winfo_width()
screen_width = window.winfo_screenwidth()
window.geometry('+%d+0' % (int((screen_width - window_width) / 2)))

window.lift()
window.attributes('-topmost',True)
window.after_idle(window.attributes,'-topmost',False)

window.mainloop()
button_cancel_click()