MCPcopy Create free account
hub / github.com/RylonW/DocNLC / applyToPIL

Method applyToPIL

utils/motionblur.py:345–383  ·  view source on GitHub ↗

[summary] Applies the kernel to an PIL.Image instance [description] converts to RGB and applies the kernel to each band before recombining them. Arguments: image {Image} -- Image to convolve keep_image_dim {b

(image: Image, keep_image_dim: bool = False)

Source from the content-addressed store, hash-verified

343 self._createKernel()
344
345 def applyToPIL(image: Image, keep_image_dim: bool = False) -> Image:
346 """[summary]
347 Applies the kernel to an PIL.Image instance
348 [description]
349 converts to RGB and applies the kernel to each
350 band before recombining them.
351 Arguments:
352 image {Image} -- Image to convolve
353 keep_image_dim {bool} -- If true, then we will
354 conserve the image dimension after blurring
355 by using "same" convolution instead of "valid"
356 convolution inside the scipy convolve function.
357
358 Returns:
359 Image -- blurred image
360 """
361 # convert to RGB
362 image = image.convert(mode="RGB")
363
364 conv_mode = "valid"
365 if keep_image_dim:
366 conv_mode = "same"
367
368 result_bands = ()
369
370 for band in image.split():
371
372 # convolve each band individually with kernel
373 result_band = convolve(
374 band, self.kernelMatrix, mode=conv_mode).astype("uint8")
375
376 # collect bands
377 result_bands += result_band,
378
379 # stack bands back together
380 result = np.dstack(result_bands)
381
382 # Get image
383 return Image.fromarray(result)
384
385 # If image is Path
386 if isinstance(image, str) or isinstance(image, Path):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected