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 :'my_image')

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

img =['my_image']

And of course none of those :
img =['my_image']

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 (‘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['my_image'].filepath_raw = '/tmp/hoopla.png'['my_image'].file_format = 'PNG'['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['my_image'].filepath_raw = '/tmp/hoopla.png'['my_image'].file_format = 'PNG'['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 =[0]
filepath = r"C:	mp\_exported_image.tif"

def find_color_mode(image):
    if not isinstance(image, bpy.types.Image):
        if image.depth <= 8:
            return 'BW'
        elif image.depth <= 24:
            return 'RGB'
            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'


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