Results 1 to 7 of 7
  1. #1
    Member
    Join Date
    Nov 2011
    Location
    France
    Posts
    46

    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 :
    Code:
    bpy.ops.image.new('my_image')
    But when it comes to saving it, none of those works :
    Code:
    bpy.ops.image.save_as(filepath='my_image')
    Code:
    img = bpy.data.images['my_image']
    img.save_render(filepath='my_image')
    And of course none of those :
    Code:
    bpy.ops.image.save()
    Code:
    img = bpy.data.images['my_image']
    img.save()
    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)
    Last edited by kevar; 14-Sep-12 at 09:05.



  2. #2
    there's a few states in which an imagine can be
    - you can make a new image ( bpy.ops.image.new('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
    Code:
    bpy.data.images['my_image'].filepath_raw = '/tmp/hoopla.png'
    bpy.data.images['my_image'].file_format = 'PNG'
    bpy.data.images['my_image'].save()
    save_render only works with renders.
    Last edited by zeffii; 17-Sep-12 at 08:37.



  3. #3
    Member
    Join Date
    Nov 2011
    Location
    France
    Posts
    46
    OK, I better understand how it works now. Thanks !



  4. #4
    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
    Code:
    bpy.data.images['my_image'].filepath_raw = '/tmp/hoopla.png'
    bpy.data.images['my_image'].file_format = 'PNG'
    bpy.data.images['my_image'].save()
    Last edited by zeffii; 14-Sep-12 at 10:19.



  5. #5
    Member
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    2,537
    zeffii: what was the original code of your post #2?
    http://blenderartists.org/forum/show...=1#post2203969

    just without setting file_format?



  6. #6
    CoDEmanX, Yeah, not setting the file_format to the same as the filepath_raw extension isn't a good idea.



  7. #7
    Member
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    2,537
    here's a script i tested working, exports to the given format unlike previous attempts setting the file_format on the image itself:

    Code:
    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •