save jpg image as tiff in addon not working

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

travnick
Posts: 3
Joined: Thu Sep 12, 2013 11:11 am

save jpg image as tiff in addon not working

Postby travnick » Thu Sep 12, 2013 11:48 am

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

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu Sep 26, 2013 4:46 pm

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!
I'm sitting, waiting, wishing, building Blender in superstition...

travnick
Posts: 3
Joined: Thu Sep 12, 2013 11:11 am

Postby travnick » Thu Sep 26, 2013 5:19 pm

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.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Fri Sep 27, 2013 2:12 am

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.
I'm sitting, waiting, wishing, building Blender in superstition...

travnick
Posts: 3
Joined: Thu Sep 12, 2013 11:11 am

Postby travnick » Fri Sep 27, 2013 9:13 am

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


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest