@@ -310,36 +310,20 @@ def invoke(self, context: InvocationContext) -> ImageOutput:
310
310
# Split the image into RGBA channels
311
311
r , g , b , a = image .split ()
312
312
313
- # Convert to float using NumPy
314
- r = numpy .array (r , dtype = numpy .float32 )
315
- g = numpy .array (g , dtype = numpy .float32 )
316
- b = numpy .array (b , dtype = numpy .float32 )
317
- a = numpy .array (a , dtype = numpy .float32 ) / 255.0 # Normalize alpha to [0, 1]
318
-
319
313
# Premultiply RGB channels by alpha
320
- r *= a
321
- g *= a
322
- b *= a
323
-
324
- # Convert back to PIL images
325
- r = Image .fromarray (numpy .uint8 (r ))
326
- g = Image .fromarray (numpy .uint8 (g ))
327
- b = Image .fromarray (numpy .uint8 (b ))
328
- a = Image .fromarray (numpy .uint8 (a * 255 )) # Denormalize alpha back to [0, 255]
329
-
330
- # Merge back into a single image
331
- premultiplied_image = Image .merge ("RGBA" , (r , g , b , a ))
314
+ premultiplied_image = ImageChops .multiply (image , a .convert ("RGBA" ))
315
+ premultiplied_image .putalpha (a )
332
316
333
317
# Apply the blur
334
318
blur = (
335
319
ImageFilter .GaussianBlur (self .radius ) if self .blur_type == "gaussian" else ImageFilter .BoxBlur (self .radius )
336
320
)
337
321
blurred_image = premultiplied_image .filter (blur )
338
322
339
- # Split the blurred image back into RGBA channels
323
+ # Split the blurred image into RGBA channels
340
324
r , g , b , a_orig = blurred_image .split ()
341
325
342
- # Convert to float using NumPy
326
+ # Convert to float using NumPy. float 32/64 division are much faster than float 16
343
327
r = numpy .array (r , dtype = numpy .float32 )
344
328
g = numpy .array (g , dtype = numpy .float32 )
345
329
b = numpy .array (b , dtype = numpy .float32 )
0 commit comments