Skip to content

Commit ded9213

Browse files
dunkeronipsychedelicious
authored andcommitted
trim blur splitting logic
1 parent 9d51eb4 commit ded9213

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

invokeai/app/invocations/image.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -310,36 +310,20 @@ def invoke(self, context: InvocationContext) -> ImageOutput:
310310
# Split the image into RGBA channels
311311
r, g, b, a = image.split()
312312

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-
319313
# 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)
332316

333317
# Apply the blur
334318
blur = (
335319
ImageFilter.GaussianBlur(self.radius) if self.blur_type == "gaussian" else ImageFilter.BoxBlur(self.radius)
336320
)
337321
blurred_image = premultiplied_image.filter(blur)
338322

339-
# Split the blurred image back into RGBA channels
323+
# Split the blurred image into RGBA channels
340324
r, g, b, a_orig = blurred_image.split()
341325

342-
# Convert to float using NumPy
326+
# Convert to float using NumPy. float 32/64 division are much faster than float 16
343327
r = numpy.array(r, dtype=numpy.float32)
344328
g = numpy.array(g, dtype=numpy.float32)
345329
b = numpy.array(b, dtype=numpy.float32)

0 commit comments

Comments
 (0)