Save image as for example .png by a script

My export script should export all images that the user might have used as textures. I don't have problems collecting the images. I also don't have a problem emulating being in an image-editor. The only thing I couldn't figure out how to do is how I can export the picture in any format. Here is a codesnippet how far I have got so far.

images is a list of images to export and they are all part of bpy.data.images. imageEditorArea is an existing image area because else the script doesn't want to run for some reason. exportDir is the directoy where all the images should go.

At this point I remember you that the user should not be able to change the loaction because he chose the location where the files should go already when he executed my operator

prevImage = imageEditorArea.spaces[0].image # want to keep the users selected image around
for image in images:
imageEditorArea.spaces[0].image = image
bpy.ops.image.save_as({'area': imageEditorArea}, # emulate an imageEditor
'INVOKE_DEFAULT', # invoke the operator
copy=True, # make a copy of the image
filepath=exportDir + image.name) # export it to this location
imageEditorArea.spaces[0].image = prevImage # reset the image back to the original


The problem now is that normally, when you don't hand over a path to export the images to, one can choose an outputformat (something like 'BMP', 'JPEG', etc...). I can't figure out how one could achieve this without opening the fileselector.

You can try to use a low-level method. The Image class is documented here.

The file format is stored in the attribute Image.file_format.

The Image class offers two methods to save images:

So you can probably save your image like this:

scene=bpy.context.scene
scene.render.image_settings.file_format='BMP'
image.save_render(filepath,scene)


You might try:

image.file_format='BMP'
bpy.ops.image.save_as({'area': imageEditorArea}, # emulate an imageEditor
'INVOKE_DEFAULT', # invoke the operator
copy=True, # make a copy of the image
filepath=exportDir + image.name) # export it to this location

• first version is better as you don't manipulate the image itself. – WorldSEnder Feb 8 '14 at 15:59