apply_filter()

Filters the display window using a preset filter or with a custom shader.

Examples

example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.THRESHOLD)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.GRAY)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.INVERT)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.POSTERIZE, 4)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.BLUR, 6)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.ERODE)
example picture for apply_filter()
1
2
3
4
def setup():
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)
    py5.apply_filter(py5.DILATE)
example picture for apply_filter()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def setup():
    py5.size(100, 100, py5.P2D)
    global blur
    global img
    blur = py5.load_shader("blur.glsl")
    img = py5.load_image("apples.jpg")
    py5.image(img, 0, 0)


def draw():
    py5.apply_filter(blur)  # blurs more each time through draw()

Description

Filters the display window using a preset filter or with a custom shader. Using a shader with apply_filter() is much faster than without. Shaders require the P2D or P3D renderer in size().

The presets options are:

  • THRESHOLD: Converts the image to black and white pixels depending if they are above or below the threshold defined by the level parameter. The parameter must be between 0.0 (black) and 1.0 (white). If no level is specified, 0.5 is used.

  • GRAY: Converts any colors in the image to grayscale equivalents. No parameter is used.

  • OPAQUE: Sets the alpha channel to entirely opaque. No parameter is used.

  • INVERT: Sets each pixel to its inverse value. No parameter is used.

  • POSTERIZE: Limits each channel of the image to the number of colors specified as the parameter. The parameter can be set to values between 2 and 255, but results are most noticeable in the lower ranges.

  • BLUR: Executes a Guassian blur with the level parameter specifying the extent of the blurring. If no parameter is used, the blur is equivalent to Guassian blur of radius 1. Larger values increase the blur.

  • ERODE: Reduces the light areas. No parameter is used.

  • DILATE: Increases the light areas. No parameter is used.

Underlying Java method: filter

Syntax

apply_filter(kind: int, /) -> None
apply_filter(kind: int, param: float, /) -> None
apply_filter(shader: Py5Shader, /) -> None

Parameters

  • kind: int - Either THRESHOLD, GRAY, OPAQUE, INVERT, POSTERIZE, BLUR, ERODE, or DILATE

  • param: float - unique for each, see above

  • shader: Py5Shader - the fragment shader to apply

Updated on September 11, 2021 16:51:34pm UTC