/blender-developer-tools:images-and-pixelsfrom blender-developer-tools
Creates and edits Blender image pixel buffers via Python: bpy.data.images.new, foreach_get/foreach_set, RGBA bottom-left layout, byte vs float storage, and saving PNG/OpenEXR with alpha. Use when writing/reading pixels, fixing TypeError, upside-down images, or alpha loss on save.
Ships in blender-developer-tools. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use this skill when the user:
bpy.data.images.new and fills it from code (test cards, masks, lookup textures, bake targets)Image.pixels, or mentions foreach_get / foreach_set, float_buffer, is_float, channelsTypeError from pixels.foreach_set / foreach_get (wrong length: RGB stride, or a buffer sized before Image.scale())Image.save()save_render wrote a PNG despite image.file_format = "OPEN_EXR"import bpy
W, H = 512, 288
img = bpy.data.images.new("Card", W, H, alpha=True) # byte (8-bit) by default
buf = [0.0] * (W * H * 4) # always RGBA, even with alpha=False
for y in range(H): # y = 0 is the BOTTOM row
for x in range(W):
i = (y * W + x) * 4
buf[i:i + 4] = (x / (W - 1), y / (H - 1), 0.0, 1.0)
img.pixels.foreach_set(buf) # one bulk write, not W*H*4 item sets
got = [0.0] * (W * H * 4)
img.pixels.foreach_get(got) # one bulk read
None of the contracts below diverge between 4.5 LTS and 5.x: the image-pixels and PNG/EXR examples assert identically on both, so no version branch is needed in image code. (If unrelated code in the same script must branch, use the bpy.app.version tuple, never version_string.)
Image.pixels is a flat, row-major float sequence, len == width * height * 4. Pixel (x, y) starts at index (y * width + x) * 4.img.channels == 4 even when created with alpha=False. A foreach_set with a W * H * 3 list raises TypeError rather than writing.foreach_set then foreach_get round trip reads back the order it wrote, so it cannot detect a flipped layout. Check orientation through a saved file or a known corner pixel.Image.scale(w, h) reallocates the buffer. Resize your Python list afterwards; a foreach_get into the old-size list raises TypeError.foreach_set / foreach_get for whole-buffer work: the testcard writes all 589,824 floats of a 512 x 288 image in one call. Indexing img.pixels[i] in a loop is the per-element path to avoid.images.new(..., float_buffer=) | is_float | Storage | Write/read round trip |
|---|---|---|---|
False (default) | False | 8 bits per channel | error up to 0.5/255, and strictly > 0 for values off the 1/255 grid |
True | True | float32 | ~1e-7 |
pixels reads and writes Python floats for both; the difference is what survives storage. A byte image keeps steps of 1/255 only.
Colorspace: set img.colorspace_settings.name = "Non-Color" for data images (masks, normal maps, lookup tables) so values are not treated as color. A default byte image is sRGB; saved to PNG and reloaded it matches the authored values at quantization tolerance. After bpy.data.images.load(path), set the loaded image's colorspace too (the PNG/EXR example sets "Non-Color" on both the source and the reload).
Alpha: the pixels API authors straight (unassociated) RGBA. That matters for float PNG, below.
import os
import tempfile
import bpy
W, H = 4, 2
rgb_only = bpy.data.images.new("SmokeRGB", W, H, alpha=False)
assert rgb_only.channels == 4 and len(rgb_only.pixels) == W * H * 4
try:
rgb_only.pixels.foreach_set([0.0] * (W * H * 3)) # RGB stride
raise AssertionError("RGB-stride foreach_set was accepted")
except TypeError:
pass
img = bpy.data.images.new("SmokeCard", W, H, alpha=True)
buf = [0.0, 0.0, 0.0, 1.0] * (W * H)
buf[0:4] = [1.0, 0.25, 0.0, 1.0] # (0, 0) = bottom-left
img.pixels.foreach_set(buf)
got = [0.0] * (W * H * 4)
img.pixels.foreach_get(got)
assert 0.0 < abs(got[1] - 0.25) <= 0.5 / 255 # 8-bit storage quantizes
img.save_render(os.path.join(tempfile.gettempdir(), "smoke_keep.png"))
assert img.source == "GENERATED" # save_render keeps the buffer
path = os.path.join(tempfile.gettempdir(), "smoke_card.png")
img.filepath_raw = path
img.file_format = "PNG"
img.save()
assert img.source == "FILE" and img.has_data # byte image: source flips, buffer kept
back = bpy.data.images.load(path)
px = [0.0] * (W * H * 4)
back.pixels.foreach_get(px)
assert abs(px[0] - 1.0) <= 0.5 / 255 and abs(px[1] - 0.25) <= 0.5 / 255
fimg = bpy.data.images.new("SmokeFloat", W, H, alpha=True, float_buffer=True)
assert fimg.is_float
fimg.pixels.foreach_set([0.1, 0.2, 0.3, 0.4] * (W * H))
fpx = [0.0] * (W * H * 4)
fimg.pixels.foreach_get(fpx)
assert abs(fpx[2] - 0.3) < 1e-6
save() vs save_render()Image.save() | Image.save_render(filepath, scene=...) | |
|---|---|---|
| Destination | img.filepath_raw | the filepath argument |
| Format from | img.file_format | the scene's render.image_settings (file_format, color_mode) |
| Ignores | the scene's render.image_settings | img.file_format |
On a GENERATED image | flips source to 'FILE'; a float image also drops its buffer (has_data False; a byte image keeps it, measured on 4.5.14 and 5.2.1) | source stays 'GENERATED', buffer untouched |
The save() trap, for float images: after it, every pixels read re-loads from whatever file is at filepath_raw now. The testcard example proves this on a float_buffer=True image by overwriting the file with a gray image and reading gray back through the original datablock. Code that writes pixels, calls save(), then keeps computing on pixels is computing on a decoded file. Use save_render() when the in-memory buffer must survive (bake targets, iterative edits), and set the scene's format for the call:
import bpy
def save_keep_buffer(image, filepath, file_format="PNG", scene=None):
scene = scene or bpy.context.scene
settings = scene.render.image_settings
previous = settings.file_format
settings.file_format = file_format # image.file_format is ignored here
try:
image.save_render(filepath, scene=scene)
finally:
settings.file_format = previous
return filepath
For exact value round-trips the PNG/EXR example uses save() with filepath_raw + file_format, so the scene's output settings stay out of the file.
Measured with Image.save() on Non-Color images:
| Source | Format | Written as | Reload vs authored straight RGBA |
|---|---|---|---|
float_buffer=True | PNG | RGBA 16-bit | each channel unpremultiplied as if associated: min(1, c / a) then 16-bit. Dark color at alpha 1/255 reloads ~white (RGB error 0.98) |
float_buffer=True | OPEN_EXR | float RGBA | within 1e-5 (measured ~3e-8) |
float_buffer=False | PNG | RGBA 8-bit, straight | per-channel 8-bit quantization only |
import os
import tempfile
import bpy
img = bpy.data.images.new("Mask", 64, 64, alpha=True, float_buffer=True)
img.colorspace_settings.name = "Non-Color"
img.pixels.foreach_set([0.02, 0.02, 0.02, 1.0 / 255.0] * (64 * 64))
img.filepath_raw = os.path.join(tempfile.gettempdir(), "mask.exr")
img.file_format = "OPEN_EXR" # float + straight alpha survive
img.save()
Rules of thumb: float data with meaningful alpha goes to OpenEXR. If it must be PNG, write it from a byte image (float_buffer=False), which stores straight alpha. Through save_render, render.image_settings.color_mode = "RGB" on OPEN_EXR drops alpha: the file reloads with alpha ~1.0.
RGB-stride buffers. [r, g, b] * (W * H) passed to foreach_set raises TypeError. The buffer is RGBA even for alpha=False.
Top-left origin. Indexing (row_from_top * W + x) * 4 flips the image. Row 0 is the bottom.
Per-element loops over img.pixels[i]. Build a flat list, write it with one foreach_set, read with one foreach_get.
Stale buffer size after scale(). Recompute W * H * 4 from the new img.size before foreach_get.
save() then reading pixels on a float image. The buffer is gone; reads come from disk. Use save_render() or reload deliberately.
Setting img.file_format and calling save_render(). It writes the scene's format (PNG on a default scene). Set scene.render.image_settings.file_format instead, and restore it.
Float image saved as PNG with alpha. Straight c > a values are unpremultiplied and clamp to white. Use OpenEXR, or a byte image for PNG.
Expecting byte images to round-trip exactly. 8-bit storage quantizes to 1/255; use float_buffer=True for data that needs precision.
Forgetting colorspace on data images and their reloads. Set "Non-Color" on both the source image and the image returned by bpy.data.images.load.
examples/image-pixels-testcard/ — always-RGBA layout, bottom-left origin proven through an independently decoded PNG, byte vs float round trip, scale() reallocation, the save() buffer drop vs save_render().examples/png-exr-alpha/ — float PNG as RGBA16 with false unpremultiply, EXR float fidelity, byte PNG straight alpha, EXR color_mode='RGB' alpha drop.snippets/save_baked_image.py — save_render with the scene format set and restored.bake-high-to-low — saving a baked image without losing it.Each example runs headless, asserts the contract, and exits non-zero when it breaks. Run one with blender --background --python <script> --; pass a falsifier flag to watch the check fail.
image-pixels-testcard: The Image pixel-buffer contract — a procedural broadcast test card written into bpy.data.images.new() with one pixels.foreach_set (589,824 floats). Falsify: --wrong-origin (exit 13).png-exr-alpha: Float-image PNG save trap — float_buffer=True Image.save() writes RGBA16 and unpremultiplies as if associated-alpha. Falsify: --opaque-alpha (exit 5).Short standalone patterns to copy. Each one runs in CI on 5.2 and 4.5 with a check and a falsifier.
bpy.types.Image: https://docs.blender.org/api/current/bpy.types.Image.htmlImage: https://docs.blender.org/api/4.5/bpy.types.Image.htmlbpy.types.BlendDataImages (images.new, images.load): https://docs.blender.org/api/current/bpy.types.BlendDataImages.htmlBlendDataImages: https://docs.blender.org/api/4.5/bpy.types.BlendDataImages.htmlbpy.types.ImageFormatSettings (render.image_settings): https://docs.blender.org/api/current/bpy.types.ImageFormatSettings.htmlImageFormatSettings: https://docs.blender.org/api/4.5/bpy.types.ImageFormatSettings.htmlbpy_prop_collection.foreach_get / foreach_set: https://docs.blender.org/api/current/bpy.types.bpy_prop_collection.htmlWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub tmhsdigital/blender-developer-tools --plugin blender-developer-tools27 skills in this plugin
/addon-scaffoldingScaffold a Blender add-on as an Extensions Platform package: blender_manifest.toml, modular file layout, register_classes_factory, symmetric register/unregister. Use when the user starts a new add-on or extension, writes or fixes blender_manifest.toml, asks which layout or registration pattern to use on Blender 4.5 LTS or 5.x, or has classes that fail to register or unregister cleanly. Targets 5.2 LTS with 4.5 LTS fallback./ai-mesh-cleanupOrdered cleanup for an imported generated or scanned mesh before it goes to an engine: scene units, apply rotation and scale, origin to base, recalculate normals, evaluated triangle count, decimate to a budget, convex collider. Use when the user has a GLB, glTF or FBX that is not engine-ready, mentions unit scale, unapplied transforms, triangle budget, LODs or a collision hull, or is about to run mesh operations on an import without checking its scale. Targets 5.2 LTS with 4.5 LTS fallback./bake-high-to-lowCage-bake high-poly surface detail onto a low-poly target as a tangent-space normal map with bpy.ops.object.bake (Cycles, selected-to-active, active Image Texture node, UV layer, save_render). Use when the user wants a normal map from a dense mesh onto a game-resolution or decimated LOD, mentions cage_extrusion or use_selected_to_active, or gets a bake that returns CANCELLED, comes out black or flat, or fails with 'No active UV layer found'. Targets 5.2 LTS with 4.5 LTS fallback./blender-rulesBlender Python anti-pattern rules (bmesh leaks, bpy.ops in loops, deprecated context-copy overrides, bulk mesh writes, prop annotations and context guards, Extensions manifest, import scale, export modifiers, glTF/FBX axis RNA, the removed bgl module, the normals API removed in 4.1). Use when writing, reviewing or fixing any bpy / Blender add-on or script code./bl-info-migrationMigrate a legacy bl_info add-on to the Extensions Platform in three steps, with a before/after diff and the dual-format pattern. Use when the user has a bl_info dictionary in __init__.py, asks whether bl_info is deprecated or still supported, wants to publish to extensions.blender.org or ship an extension .zip, or sees warnings about legacy add-ons at install time. Targets 5.2 LTS; manifests work from 4.2./compositor-nodesBuild and render through the scene compositor from Python across the 4.5 LTS to 5.x split: scene.use_nodes plus scene.node_tree and a Composite node on 4.5, a CompositorNodeTree node group assigned to scene.compositing_node_group with interface sockets and a Group Output on 5.x, a Render Layers to Glare (Fog Glow) chain, the Glare node's move from enum properties to menu input sockets, and render.use_compositing. Use when the user scripts compositor nodes, adds bloom or glow, hits AttributeError on scene.node_tree in 5.x, sets glare_type or quality and gets AttributeError on 5.x, looks for the removed EEVEE use_bloom toggle, or renders and sees no compositor effect. Targets 5.2 LTS with 4.5 LTS fallback.Other plugins' skills for the same job