Saving new image.[Solved]

Hi.

I’m having a bit of a problem with saving images that I’ve created with Image.New()

Here is some code…


import Blender
from Blender import *

newimg = Image.New('myimage',256,256,24)
#Put one pixel in image
newimg.setPixelF(128,128,(1.0,1.0,1.0,1.0))
newimg.setFilename('c:/myimage.png')
newimg.save()

This creates a 2kb file called myimage.png on the disk but the image can’t be viewed in any viewer i’ve tried. Judging by the content of the file, it doesn’t seem to be png either but rather some raw data.

What am I doing wrong?

replacing Image.New() with Image.Load() works fine, but the idea is that I’m supposed to create the image myself.

Looking at the BPy source, it appears the file type is not getting set. ( the type is not the same thing as the file extension, no matter what some OSes assume).

Your next question, no doubt, is “How to set the file type?” Sadly, that seems to be something missing from the Image module.

Anticipating your next question, I can only assume whoever implemented the Image module cared more about loading images than creating them from scratch.

Image.save() saves targa

saving images should be possible as i succeeded combining two loaded images into one created by code

Yep, it was targa.

I simply made the mistake of assuming Blender would set the format based on the file extension.

I’ve tried opening the file in Gimp(which handles targa) but Gimp seems to go by the file extension and therefore identified the image as a png.
Changed the file extension to .tga and the problem was solved. :wink:

Thanks for the help. :slight_smile:

I think you have to set the type in the render context when you save the image,

That works when rendering an image, but I think the image I’m creating from scratch has nothing to do with the rendering context. :expressionless:

Running this still outputs a targa file.

import Blender
from Blender import *

context = Scene.GetCurrent().getRenderingContext()
context.enableExtensions(1)
context.setImageType(Scene.Render.PNG)

newimg = Image.New('myimage',256,256,24)
newimg.setPixelF(128,128,(1,1,1,1))
newimg.setFilename('c:/myimage.png')
newimg.save()