Saving bitmap to png with alpha [SOLVED]



  • On 13/02/2013 at 02:26, xxxxxxxx wrote:

    Hi,

    I have trouble saving a render to PNG with alpha layer. The resulting PNG image shows no transparency. If I render the model directly from c4d, the resulting image is fine.
    I tried to use c4d.SAVEBIT_ALPHA in the Save function but it makes the script crash (see below)

    What I am doing wrong?

      
    doc = c4d.documents.GetActiveDocument()  
      rd = doc.GetActiveRenderData().GetData()  
      rd[c4d.RDATA_ALPHACHANNEL]= 1  
      xres = int(round(rd[c4d.RDATA_XRES]))  
      yres = int(round(rd[c4d.RDATA_YRES]))  
      
      bmp = bitmaps.BaseBitmap()  
      bmp.Init(xres, yres, depth=32)  
      
      res = documents.RenderDocument(doc, rd, bmp, c4d.RENDERFLAGS_EXTERNAL)  
      if res == c4d.RENDERRESULT_OK:  
          bitmaps.ShowBitmap(bmp)  
          bmp.Save(filename, c4d.FILTER_PNG)  
          # this line makes the script crash at BaseContainer.cpp:1095: bar argument to internal function  
          #bmp.Save(filename, c4d.FILTER_PNG, c4d.SAVEBIT_ALPHA)  
      
    

    Thanks

    Alex



  • On 13/02/2013 at 04:02, xxxxxxxx wrote:

    Hi Alex,

    the documentation seems to be wrong. The data argument is first, then the savebits flags follow.
    I found this information in the C++ Documentation.

      
    import c4d
    from c4d import bitmaps, documents
      
    def main() :
        filename = "C:\\foo.png"
        doc = c4d.documents.GetActiveDocument()
        rd = doc.GetActiveRenderData().GetData()
        rd[c4d.RDATA_ALPHACHANNEL]= 1
        xres = int(round(rd[c4d.RDATA_XRES]))
        yres = int(round(rd[c4d.RDATA_YRES]))
      
        bmp = bitmaps.BaseBitmap()
        bmp.Init(xres, yres, depth=32)
      
        res = documents.RenderDocument(doc, rd, bmp, c4d.RENDERFLAGS_EXTERNAL)
        if res == c4d.RENDERRESULT_OK:
            bitmaps.ShowBitmap(bmp)
            bmp.Save(filename, c4d.FILTER_PNG, c4d.BaseContainer(), c4d.SAVEBIT_ALPHA)
      
    main()
    

    For future posts, please make sure the add an SSCCE with your question. It's always irritating
    when having to make the example actually run before we can try to help you.

    -Niklas



  • On 13/02/2013 at 04:20, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    the documentation seems to be wrong. The data argument is first, then the savebits flags follow.
    I found this information in the C++ Documentation.

    Please always be sure to use the latest documentation. This was recently fixed.



  • On 13/02/2013 at 10:41, xxxxxxxx wrote:

    @Yannick Puech: Sorry I forgot to mention I am using R13. Last documentation update was in May, if I am not mistaken?

    @NiklasR:
    Thanks a lot, that fixed the error. I could have spent a day on that!

    However I couldn't find a way to save a PNG with alpha using the bitmap class. I found a workaround, I'll investigate later.

    Alex



  • On 18/01/2015 at 16:20, xxxxxxxx wrote:

    @Al3d
    Hi, I ran into the same problem - no transparency in the png file that was saved via the BaseBitmap() class. This is what i am using on a scene with a simple cube:

    import c4d
    from c4d import bitmaps, documents
      
    def main() :
        doc = c4d.documents.GetActiveDocument()
        rd = doc.GetActiveRenderData().GetData()
        rd[c4d.RDATA_ALPHACHANNEL]= 1
        xres = int(round(rd[c4d.RDATA_XRES]))
        yres = int(round(rd[c4d.RDATA_YRES]))
        
        filename = rd[c4d.RDATA_PATH]+'_alpha_test.png'
        bmp = bitmaps.BaseBitmap()
        bmp.Init(xres, yres, depth=32)
        bmp.AddChannel(1,0)
      
        res = documents.RenderDocument(doc, rd, bmp, c4d.RENDERFLAGS_EXTERNAL)
        if res == c4d.RENDERRESULT_OK:
            bitmaps.ShowBitmap(bmp)
            bmp.Save(filename, rd[c4d.RDATA_FORMAT], c4d.BaseContainer(), c4d.SAVEBIT_ALPHA)
    main()
    

    In my render settings, i selected a save path, image format=png and alphachannel=true
    Any idea how to save a png with transparency using BaseBitmap?

    Thanks, Sebastian



  • On 20/01/2015 at 05:30, xxxxxxxx wrote:

    Hi Sebastian,

    actually the problem is not saving the bitmap. If you look closely the alpha is already missing in picture manager.
    The secret is to use MultipassBitmap instead.
    Like so:

            bmp = bitmaps.MultipassBitmap(xres, yres, rd[c4d.RDATA_FORMATDEPTH])
            bmp.AddAlpha(bmp.GetLayers(c4d.MPB_GETLAYERS_0), c4d.COLORMODE_ALPHA)
    

    I'll have this info added to the Python docs (the C++ SDK docs already contain a note on this).



  • On 21/01/2015 at 02:47, xxxxxxxx wrote:

    Hi Andreas,
    Thanks, that solved the problem. Saving PNGs with transparent background works fine like this.

    Cheers, Sebastian



  • On 12/07/2015 at 06:43, xxxxxxxx wrote:

    Looking everywhere for C++ info on this.  I am trying to create a BaseBitmap to use RenderDocument on with an alpha.  Only creating the BaseBitmap in 32bit doesn't show a COLORMODE of ARGB.  Tried adding an alpha by AddChannel but that does not work either.  It seems the following applies.  Could you point me to the C++ documentation that explains this?

    Thank you,
    Ama

    >Originally posted by xxxxxxxx

    Hi Sebastian,

    actually the problem is not saving the bitmap. If you look closely the alpha is already missing in picture manager.
    The secret is to use MultipassBitmap instead.
    Like so:

            bmp = bitmaps.MultipassBitmap(xres, yres, rd[c4d.RDATA_FORMATDEPTH])
            bmp.AddAlpha(bmp.GetLayers(c4d.MPB_GETLAYERS_0), c4d.COLORMODE_ALPHA)
    

    I'll have this info added to the Python docs (the C++ SDK docs already contain a note on this).



  • On 13/07/2015 at 04:47, xxxxxxxx wrote:

    Hi Ama,

    in C++ it's actually pretty similar to Python, see here our docs on RenderDocument() and here on MultipassBitmap. For more discussion on how to work with MultipassBitmap, I recommend to open a new thread in SDK Help subforum.


Log in to reply