I'm currently trying to extract textures from a binary file, and ideally I'd like to save each texture to a file or embed in the .blend file. However, before saving an image, I need to be able to create an image in memory from an array of pixel data.

The PIL library contains the kinds of functions I need to use, but it is not included with Blender. Is there anything similar to PIL in Blender? bpy.ops.image seems to be close to what I need, but lacks necessary pixel set/manipulation functions.


Yes its possible, heres an example

size = 640, 480

import bpy
# blank image
image = bpy.data.images.new("MyImage", width=size[0], height=size[1])

## For white image
# pixels = [1.0] * (4 * size[0] * size[1])

pixels = [None] * size[0] * size[1]
for x in range(size[0]):
    for y in range(size[1]):
        # assign RGBA to something useful
        r = x / size[0]
        g = y / size[1]
        b = (1 - r) * g
        a = 1.0

        pixels[(y * size[0]) + x] = [r, g, b, a]

# flatten list
pixels = [chan for px in pixels for chan in px]

# assign pixels
image.pixels = pixels

# write image
image.filepath_raw = "/tmp/temp.png"
image.file_format = 'PNG'

output of script

| improve this answer | |
  • $\begingroup$ With this technic is it possible to save the image with RGBA I made some test but I can't find how to do it directly with python? $\endgroup$ – lucblender Dec 20 '15 at 10:48
  • $\begingroup$ Do you mean doing this from regular Python? (which would work outside of Blender for example) $\endgroup$ – ideasman42 Dec 20 '15 at 12:57
  • $\begingroup$ Nop inside of blender, I find the answer by myself. I can use image.save_render(...) instead of image.save() and it will take the render parameter $\endgroup$ – lucblender Dec 20 '15 at 13:10
  • $\begingroup$ If you want to save PNG images without using Blender's API, see this answer: stackoverflow.com/questions/902761/… $\endgroup$ – ideasman42 Aug 27 '16 at 1:48

enter image description here

And this is if we are using Sverchok Add-On and nodes in Sverchok. I am translating IdeasMan42 example. Neat.

| improve this answer | |
  • 1
    $\begingroup$ If that's so neat, can you post a high-res image of your tree? Hard to see what you are doing here... $\endgroup$ – brockmann Mar 11 '17 at 12:10
  • $\begingroup$ Sorry low resolution image. The last node is Texture Viewer. Everything else is Map Range and some Math node. The green node is Plane. Give it a try :) $\endgroup$ – Blender Sushi Guy Mar 12 '17 at 12:17
  • $\begingroup$ GIST: b6223f0152a98d88bd8a130562419c2d $\endgroup$ – Blender Sushi Guy Mar 12 '17 at 12:19
  • 3
    $\begingroup$ Gist as real url: gist.github.com/anonymous/b6223f0152a98d88bd8a130562419c2d $\endgroup$ – brockmann Mar 15 '17 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.