Learn creative coding writing simple programs

127. Fun with filters part I

filter() is used in Processing to transform the currently displayed image. It takes one or two arguments.

The first argument can be a shader or a constant that indicates the kind of filter you want to apply. The available filters are THRESHOLD, GRAY, OPAQUE, INVERT, POSTERIZE, BLUR, ERODE, and DILATE. Shaders are very fast and powerful, but involve learning a new programming language, so we will leave that for later.

The second argument is optional and it is used to adjust some kind of setting on the filter. For example filter(BLUR, 6) means use the "blur" filter, and the blur should be 6 pixels wide.

Filters can be applied in series: first one, then another. They can also be applied multiple times, for example once each time draw() is called. Applying THRESHOLD, GRAY or POSTERIZE multiple times on the same exact image does not make much sense, as a gray image can not be "grayer". But if something does change in the image then it may make sense to apply again and again.

The amount of available filters is limited, but combining them in creative ways one can obtain interesting images.

Check out episodes 89 and 90 if you want to build your own custom filters.

Tags: filter, ternary, blur, threshold, invert, gray, posterize, erode, dilate

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```