Spaces:
Runtime error
Runtime error
Upload kuwahara.py
Browse files- kuwahara.py +85 -0
kuwahara.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import bpy
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def apply_kuwahara(
|
| 9 |
+
image: Image.Image,
|
| 10 |
+
size: int = 5,
|
| 11 |
+
eccentricity: float = 2,
|
| 12 |
+
sharpness: float = 1,
|
| 13 |
+
uniformity: int = 10,
|
| 14 |
+
) -> Image.Image:
|
| 15 |
+
"""
|
| 16 |
+
Apply Blender's anisotropic Kuwahara filter using a temporary file.
|
| 17 |
+
This is reliable in headless/background mode.
|
| 18 |
+
"""
|
| 19 |
+
# ---------- 1. Setup temp files ----------
|
| 20 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_in:
|
| 21 |
+
in_path = tmp_in.name
|
| 22 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_out:
|
| 23 |
+
out_path = tmp_out.name
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Save input PIL image to disk so Blender can load it normally
|
| 27 |
+
image.save(in_path, format="PNG")
|
| 28 |
+
|
| 29 |
+
# ---------- 2. Setup scene and compositor ----------
|
| 30 |
+
scene = bpy.context.scene
|
| 31 |
+
scene.use_nodes = True
|
| 32 |
+
tree = scene.node_tree
|
| 33 |
+
tree.nodes.clear()
|
| 34 |
+
|
| 35 |
+
scene.render.engine = 'BLENDER_EEVEE_NEXT'
|
| 36 |
+
scene.render.compositor_device = 'CPU'
|
| 37 |
+
scene.render.image_settings.file_format = 'PNG'
|
| 38 |
+
scene.render.image_settings.color_mode = 'RGB'
|
| 39 |
+
|
| 40 |
+
# Create compositor nodes
|
| 41 |
+
img_node = tree.nodes.new('CompositorNodeImage')
|
| 42 |
+
kuwahara_node = tree.nodes.new('CompositorNodeKuwahara')
|
| 43 |
+
comp_node = tree.nodes.new('CompositorNodeComposite')
|
| 44 |
+
size_node = tree.nodes.new('CompositorNodeValue')
|
| 45 |
+
|
| 46 |
+
size_node.outputs[0].default_value = float(size)
|
| 47 |
+
|
| 48 |
+
kuwahara_node.variation = 'ANISOTROPIC'
|
| 49 |
+
kuwahara_node.eccentricity = eccentricity
|
| 50 |
+
kuwahara_node.sharpness = sharpness
|
| 51 |
+
kuwahara_node.uniformity = uniformity
|
| 52 |
+
|
| 53 |
+
tree.links.new(img_node.outputs[0], kuwahara_node.inputs[0])
|
| 54 |
+
tree.links.new(size_node.outputs['Value'], kuwahara_node.inputs['Size'])
|
| 55 |
+
tree.links.new(kuwahara_node.outputs[0], comp_node.inputs[0])
|
| 56 |
+
|
| 57 |
+
# ---------- 3. Load image into Blender ----------
|
| 58 |
+
bl_image = bpy.data.images.load(in_path)
|
| 59 |
+
img_node.image = bl_image
|
| 60 |
+
|
| 61 |
+
# ---------- 4. Set render resolution and render ----------
|
| 62 |
+
scene.render.resolution_x = bl_image.size[0]
|
| 63 |
+
scene.render.resolution_y = bl_image.size[1]
|
| 64 |
+
scene.render.resolution_percentage = 100
|
| 65 |
+
scene.render.filepath = out_path
|
| 66 |
+
|
| 67 |
+
# Render to out_path
|
| 68 |
+
bpy.ops.render.render(write_still=True)
|
| 69 |
+
|
| 70 |
+
# ---------- 5. Read result back into PIL ----------
|
| 71 |
+
result_image = Image.open(out_path).convert('RGB')
|
| 72 |
+
result_image.load() # Force load pixel data so we can delete the file safely
|
| 73 |
+
|
| 74 |
+
finally:
|
| 75 |
+
# Cleanup Blender image datablock
|
| 76 |
+
if 'bl_image' in locals():
|
| 77 |
+
bpy.data.images.remove(bl_image)
|
| 78 |
+
|
| 79 |
+
# Delete temporary files
|
| 80 |
+
if os.path.exists(in_path):
|
| 81 |
+
os.unlink(in_path)
|
| 82 |
+
if os.path.exists(out_path):
|
| 83 |
+
os.unlink(out_path)
|
| 84 |
+
|
| 85 |
+
return result_image
|