10
$\begingroup$

I have tried the following, and the resulting image file, when opened in GIMP, never contains an alpha channel, only R, G and B.

import bpy
image = bpy.data.images.new("Sprite", alpha=True, width=16, height=16)
image.filepath = "//Sprite.png"
image.file_format = 'PNG'
image.save()

Result: no alpha channel in png

import bpy
image = bpy.data.images.new("Sprite", alpha=True, width=16, height=16)
image.colorspace_settings.name = 'sRGB'
image.use_alpha = True
image.alpha_mode = 'STRAIGHT'
image.filepath = "//Sprite.png"
image.file_format = 'PNG'
image.save()

Result: no alpha channel in png file.

$\endgroup$

1 Answer 1

10
$\begingroup$

This is a bug in Blender, I've reported it on your behalf.

Setting image.colorspace_settings.name changes the alpha at the moment, reported (now fixed!)

This script works, notice it sets filepath_raw, which prevents reloading to the new path.

import bpy
image = bpy.data.images.new("Sprite", alpha=True, width=16, height=16)
image.use_alpha = True
image.alpha_mode = 'STRAIGHT'
image.filepath_raw = "//Sprite.png"
image.file_format = 'PNG'
image.save()
$\endgroup$
4
  • $\begingroup$ Thank you. I started to suspect that this was a bug. As a temporary solution I was temporarily set an empty layer and the render file path to do an empty render with ops.render.render(write_still=True) to produce an empty png with alpha channel and then create a temp image pointing to that file and reload. But if filepath_raw works I will do that from now on. $\endgroup$ Commented Aug 7, 2013 at 21:56
  • 1
    $\begingroup$ @ideasman42 I'm trying to use your code to save to OpenEXR. It works, but how do I specify the exr_codec when saving? My files are saving without a codec, so the file sizes are huge. I'd like to use the ZIP codec. $\endgroup$
    – Justin
    Commented Sep 23, 2016 at 21:30
  • $\begingroup$ How to set a relative subfolder: //texture/sprites.png doesn't work. $\endgroup$ Commented Jul 20, 2017 at 17:52
  • 1
    $\begingroup$ Prefer handle other issues as separate questions, feel free to link to them in comments here. $\endgroup$
    – ideasman42
    Commented Jul 21, 2017 at 4:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .