Below is an example of how you can set up a Blender compositor node‐tree that uses the new Kuwahara node (introduced in Blender 4.4) entirely from a Python script that you can run on Google Colab. In this example the script:
- Installs and calls Blender in background (‑‑background mode) from Colab.
- Loads an input image (which you must upload to Colab).
- Sets up the compositor with an Image node, the Kuwahara node (with all its options exposed as properties), and finally a Composite node.
- Renders the result to disk.
You can adjust all of the Kuwahara node options as desired. (Note that the actual properties available on the node may vary slightly as Blender develops; consult the Blender Python API Reference for your exact version if needed.)
Below are the complete code cells and the Python script. (You can paste these into separate cells in your Colab notebook.)
1. Install Blender
In a cell run the following bash commands to update apt and install Blender (ensuring version 4.4 is installed or higher). If you need a specific Blender version binary, you might instead download it from Blender’s website and adjust the path accordingly.
%%bash
sudo apt-get update
sudo apt-get install -y blender
2. Upload Your Input Image
Ensure you have an image—for example, input.png—uploaded to your Colab working directory. To do so, you can use Colab’s file upload widget:
from google.colab import files
uploaded = files.upload() # select your "input.png" file from your computer
3. Create the Blender Python Script
Create a file named kuwahara_compositor.py that will set up the scene, add nodes, adjust all Kuwahara options, and then render the result (saving it as output.png). Open a new cell and run the following Python code to write the script:
%%writefile kuwahara_compositor.py
import bpy
import os
# -----------------------------
# Helper: Clean existing nodes
# -----------------------------
def clear_node_tree(tree):
for node in tree.nodes:
tree.nodes.remove(node)
# -------------
# Setup Scene
# -------------
scene = bpy.context.scene
scene.use_nodes = True
tree = scene.node_tree
# Clear default nodes.
clear_node_tree(tree)
# ----------------------------
# Create nodes and configure them
# ----------------------------
# 1. Image Node: load your input image.
image_node = tree.nodes.new(type="CompositorNodeImage")
# Replace "input.png" with your file name (the file should be in the working directory)
input_image_path = os.path.abspath("input.png")
try:
image_node.image = bpy.data.images.load(input_image_path)
except Exception as e:
raise RuntimeError(f"Failed to load image from {input_image_path} - {e}")
# 2. Kuwahara Node: add the node and set all options.
# (As of Blender 4.4, the Kuwahara node has several properties. The ones below are examples;
# adjust names/values as needed.)
kuwahara_node = tree.nodes.new(type="CompositorNodeKuwahara")
# --- Kuwahara Options ---
# (Check the Blender API for additional or renamed properties.)
# Option: "size" controls the filter window size (the kernel radius).
kuwahara_node.size = 5 # Example: kernel radius (integer)
# Option: "iterations" controls how many times the filter is iterated.
kuwahara_node.iterations = 2 # Example: set to 2 iterations
# Option: "mode" may let you choose between available filtering modes.
# (For example, available enum items might be: 'MIN', 'MAX', 'MEAN', etc.
# Adjust the string values as needed—run `print(kuwahara_node.bl_rna.properties['mode'].enum_items.keys())`
# in the Blender Python console to see available modes.)
kuwahara_node.mode = 'MEAN' # Example: use the average (mean) mode
# Option: "use_advanced" (if available) to enable extra processing.
# Note: This property name is hypothetical—check your Blender API.
if "use_advanced" in kuwahara_node.bl_rna.properties:
kuwahara_node.use_advanced = True
# Option: "threshold" (if available) to set an edge-preservation threshold.
if "threshold" in kuwahara_node.bl_rna.properties:
kuwahara_node.threshold = 0.1
# Option: "clamp" (if available) to clamp extreme values.
if "clamp" in kuwahara_node.bl_rna.properties:
kuwahara_node.clamp = 1.0
# (Add or adjust any additional Kuwahara options available in your Blender version)
# 3. Composite Node: used to output the final result.
composite_node = tree.nodes.new(type="CompositorNodeComposite")
# Optionally, add a Viewer Node (so you can see a preview in Blender's UI if needed).
# (In background mode this node isn’t strictly necessary.)
viewer_node = tree.nodes.new(type="CompositorNodeViewer")
viewer_node.name = "Viewer"
# ---------------
# Arrange Nodes
# ---------------
# (This helps line up the nodes in the node editor—arrangement is optional.)
image_node.location = (-300, 0)
kuwahara_node.location = (0, 0)
composite_node.location = (300, 100)
viewer_node.location = (300, -100)
# ----------------
# Link the nodes:
# ----------------
links = tree.links
links.new(image_node.outputs["Image"], kuwahara_node.inputs[0])
links.new(kuwahara_node.outputs["Image"], composite_node.inputs["Image"])
links.new(kuwahara_node.outputs["Image"], viewer_node.inputs["Image"])
# ---------------
# Render Settings
# ---------------
# Set render resolution (optional)
scene.render.resolution_x = 640
scene.render.resolution_y = 480
# Set the output path for the composited image
scene.render.filepath = os.path.abspath("output.png")
# -------------------------------------
# Execute the compositor (render pass)
# -------------------------------------
# We use 'render' with write_still=True to execute the compositor.
bpy.ops.render.render(write_still=True)
print("Rendering complete! Check output.png")
This script does the following:
- It forces the scene to use nodes and wipes out any default nodes.
- It creates an Image node (loading
"input.png" from the current directory), a Kuwahara node where you can adjust the sample parameters (all available options are shown and conditioned on their existence), and finally a Composite (and Viewer) node to output the result.
- It sets render resolution and output path, then calls the render operator (which processes the node‐tree).
Tip: If you are unsure which options are available on your specific version’s Kuwahara node, you can open Blender’s Python console (or insert a temporary print(kuwahara_node.bl_rna.properties.keys()) in the script) to inspect them.
4. Run the Blender Script from Colab
Now, in a new cell run the following command to call Blender in background mode with your script:
!blender --background --python kuwahara_compositor.py
After execution the compositor is run, and you should find an output.png file in your Colab working directory. You can download it using:
from google.colab import files
files.download("output.png")
Final Notes
-
Adjusting Options:
In the script the Kuwahara node’s options (such as size, iterations, mode, etc.) are explicitly set. You can change these values (or add new ones, checking with if "property_name" in kuwahara_node.bl_rna.properties:) to experiment with different effects.
-
Blender Version:
This script assumes Blender 4.4 or later is installed. If you need a specific version, you may download it from Blender’s website and adjust your Colab install accordingly.
-
File Paths:
All file paths in the script are absolute (using os.path.abspath) so that Blender (running from the Colab working directory) finds them correctly.
With these steps you have an easy-to-use implementation for using Blender’s 4.4 compositor Kuwahara node on Google Colab with all its available options adjustable from the script. Enjoy experimenting with the effect!