Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Saving bitmap to png with alpha [SOLVED]

Author
Message
  Topic Search Topic Search
Al3d View Drop Down
Member
Member


Joined: 2012 Jul 23
Online Status: Offline
Posts: 12
Direct Link To This Post Topic: Saving bitmap to png with alpha [SOLVED]
    Posted: 2013 Feb 13 at 2:26am
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
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2013 Feb 13 at 4:02am
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


Edited by NiklasR - 2013 Feb 13 at 4:08am
Back to Top
Yannick Puech View Drop Down
Forum Moderator
Forum Moderator


Joined: 2011 Apr 13
Location: Spain
Online Status: Offline
Posts: 1143
Direct Link To This Post Posted: 2013 Feb 13 at 4:20am
Originally posted by NiklasR

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.


MAXON
Developer Support
Back to Top
Al3d View Drop Down
Member
Member


Joined: 2012 Jul 23
Online Status: Offline
Posts: 12
Direct Link To This Post Posted: 2013 Feb 13 at 10:41am
@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
Back to Top
seppsan View Drop Down
Member
Member


Joined: 2015 Jan 18
Online Status: Offline
Posts: 2
Direct Link To This Post Posted: 2015 Jan 18 at 4:20pm
@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


Edited by seppsan - 2015 Jan 19 at 5:03pm
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2015 Jan 20 at 5:30am
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).



Edited by Andreas Block - 2015 Jan 20 at 12:21pm
Cheers,
Andreas
SDK Support Engineer
Back to Top
seppsan View Drop Down
Member
Member


Joined: 2015 Jan 18
Online Status: Offline
Posts: 2
Direct Link To This Post Posted: 2015 Jan 21 at 2:47am
Hi Andreas,
Thanks, that solved the problem. Saving PNGs with transparent background works fine like this.

Cheers, Sebastian
Back to Top
knottypixel View Drop Down
Member
Member
Avatar

Joined: 2013 Dec 21
Online Status: Offline
Posts: 75
Direct Link To This Post Posted: 2015 Jul 12 at 6:43am
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 Andreas Block

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).




Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2015 Jul 13 at 4:47am
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.
Cheers,
Andreas
SDK Support Engineer
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.117 seconds.