Page 1 of 1

save jpg image as tiff in addon not working

Posted: Thu Sep 12, 2013 11:48 am
by travnick
HIi

I'm developing blender exporter for CryEngine - CryBlend.

I need to convert an image to tiff, and then pass it to cryengine resource compiled. The thing is while saving image with blender python console, then it works:

Code: Select all

image2 = bpy.data.images[1].copy()
image2.filepath_raw = 'new_path.tif'
image2.file_format = 'TIFF'
image2.save()
but the same code placed in addon throws an exception at save()
"RuntimeError: Error: Image "my image" does not have any image data."

What's more, while debugging that code with pydev then it works as expected.

I'm totally puzzled with it.
here is the code.
export.py:1196
method __convert_to_tiff_and_save

Posted: Thu Sep 26, 2013 4:46 pm
by CoDEmanX
hey travnick,

sorry for the late reply, but i couldn't remember where i saw your post.

I tested this working:

Code: Select all

import bpy

img = bpy.data.images[0]
filepath = r"C:\tmp\_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
Not entirely sure about the color depth and mode stuff, but you get the idea. No need to change file_format on the image itself, change the render settings instead!

Posted: Thu Sep 26, 2013 5:19 pm
by travnick
Quite good idea to use a render image.

I'm doing it now with manipulating original image:
1. change format
2. set raw_filepath
3. save
4. restore originals.

Posted: Fri Sep 27, 2013 2:12 am
by CoDEmanX
does that actually work? I tried it, and it created the file with desired extension, but the content wasn't in the right format.

save_render() doesn't render btw., it means it uses some of the scene render settings to store the output! If you set the render settings to TIFF, you can save an image as TIFF, no matter what it was before. And you don't have to duplicate the datablock.

Posted: Fri Sep 27, 2013 9:13 am
by travnick
It works actually but modifying the original image is a bad thing in my point of view so I'll try your solution with renderer.

Here is the working code: https://github.com/travnick/CryBlend/bl ... ter.py#L70