# Is it possible to create image data and save to a file from a script

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'
image.save()


• 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? Dec 20, 2015 at 10:48
• Do you mean doing this from regular Python? (which would work outside of Blender for example) Dec 20, 2015 at 12:57
• 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 Dec 20, 2015 at 13:10
• If you want to save PNG images without using Blender's API, see this answer: stackoverflow.com/questions/902761/… Aug 27, 2016 at 1:48
• A question was asked what is chan for in the line pixels = [chan for px in pixels for chan in px] - the line is an equivalent to pixels = [] \n for chan in px: \n\t for px in pixels: \n\t\t pixels.append(px) where \n and \t mean a newline and a tab. Feb 13 at 20:03

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

• If that's so neat, can you post a high-res image of your tree? Hard to see what you are doing here... Mar 11, 2017 at 12:10
• 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 :) Mar 12, 2017 at 12:17
• GIST: b6223f0152a98d88bd8a130562419c2d Mar 12, 2017 at 12:19
• Gist as real url: gist.github.com/anonymous/b6223f0152a98d88bd8a130562419c2d Mar 15, 2017 at 15:02