Hello,
I am trying to write/read a C4D-rendered image file to & from a string. In this example, I am able to use Base64 to write the image but I have to save to disk in order to convert it to a string. The same goes for reading it back from a string to a BaseBitmap
.
- How can I go straight from the rendered bitmap in memory to a string without having to save to disk?
- How can I read the string back into memory and initialize a
BaseBitmap
without having to save the image again to disk as I'm doing here?
import c4d, os, base64
from c4d import storage
desktop = storage.GeGetC4DPath(c4d.C4D_PATH_DESKTOP)
rd = doc.GetActiveRenderData()
imageString = ""
def ReadImage(b64str):
image_64_decode = base64.decodestring(b64str)
bmp2Path = os.path.join(desktop,"Test.jpg")
with open(bmp2Path, "rb") as imageFile:
imageString = base64.b64encode(imageFile.read())
print imageString
image_result = open(bmp2Path, 'wb')
image_result.write(image_64_decode)
bmp = c4d.bitmaps.BaseBitmap()
bmp.InitWith(bmp2Path)
def WriteImage(bmp,bmpPath):
if bmp.Save(bmpPath, c4d.FILTER_JPG) == c4d.IMAGERESULT_OK:
print "Bitmap saved to: %s"%bmpPath
with open(bmpPath, "rb") as imageFile:
imageString = base64.b64encode(imageFile.read())
return imageString
return False
def main(doc):
bmp = c4d.bitmaps.BaseBitmap()
bmp.Init(250,250,24)
bmpPath = os.path.join(desktop,"Test.jpg")
if c4d.documents.RenderDocument(doc, rd.GetData(), bmp, c4d.RENDERFLAGS_EXTERNAL) != c4d.RENDERRESULT_OK:
raise RuntimeError("Failed to render the document.")
else:
imgString = WriteImage(bmp,bmpPath)
ReadImage(imgString)
if __name__=='__main__':
main(doc)
Thank you.