Saving a newly created image

Hi !

I’m having a bit of trouble with a quite simple process : i’d like to create and save an image file only with python. To create an image, it’s fairly easy :

bpy.ops.image.new('my_image')

But when it comes to saving it, none of those works :

bpy.ops.image.save_as(filepath='my_image')
img = bpy.data.images['my_image']
img.save_render(filepath='my_image')

And of course none of those :

bpy.ops.image.save()
img = bpy.data.images['my_image']
img.save()

And I haven’t found in the API doc another way to save an image. How can I do that ?

Thanks a lot !

(I’m using Blender 2.63)

there’s a few states in which an imagine can be

  • you can make a new image ( bpy.ops.image.new(‘my_image’) ) | type=‘UV_TEST’
  • you can render an image ( [‘Render Result’] unless you rename it ) | type=‘RENDER_RESULT’
  • an image can be loaded from an external source. | type=‘IMAGE’

An image of type uv test ( ie, new image ) , you must set the filepath_raw and file_format first


bpy.data.images['my_image'].filepath_raw = '/tmp/hoopla.png'
bpy.data.images['my_image'].file_format = 'PNG'
bpy.data.images['my_image'].save()

save_render only works with renders.

OK, I better understand how it works now. Thanks !

There is a slight problem with my suggestion, if you create an image from blender the type is TGA / TARGA, when you use my suggestion it will save the image as a .png but it will actually be a .TGA. and it will produce files that image editors aren’t expecting. I’m still looking into it, because i use this method for my image uploader too.

you have to do


bpy.data.images['my_image'].filepath_raw = '/tmp/hoopla.png'
bpy.data.images['my_image'].file_format = 'PNG'
bpy.data.images['my_image'].save()

CoDEmanX, Yeah, not setting the file_format to the same as the filepath_raw extension isn’t a good idea.

here’s a script i tested working, exports to the given format unlike previous attempts setting the file_format on the image itself:

import bpy

img = bpy.data.images[0]
filepath = r"C:	mp\_exported_image.tif"


def find_color_mode(image):
    if not isinstance(image, bpy.types.Image):
        raise(TypeError)
    else:
        if image.depth <= 8:
            return 'BW'
        elif image.depth <= 24:
            return 'RGB'
        else:
            return 'RGBA'


settings = bpy.context.scene.render.image_settings
format = settings.file_format
mode = settings.color_mode
depth = settings.color_depth

settings.file_format = 'TIFF'
settings.color_mode = find_color_mode(img)
settings.color_depth = '8'

img.save_render(filepath)

settings.file_format = format
settings.color_mode = mode
settings.color_depth = depth