On 14/05/2015 at 15:10, xxxxxxxx wrote:
Hi,
I try to write an plugin that split a image into a image with layer.
Each color (material id's) get it own layer.
The layer are convert to use for alphamaps in c4d material.
I can initialize a bitmap from a file.
But it is BaseBitmap.
I try to use MultipassBitmap.
Now I can create a new empty layer.
But I cant set Pixel at the empty layer.
How can I set pixel to a layer?
import c4d
import os
from c4d import bitmaps, gui, storage
def main() :
doc = c4d.documents.GetActiveDocument()
op = doc.GetActiveObject()
path = storage.LoadDialog(type=c4d.FILESELECTTYPE_IMAGES, title="Please Choose the PolyPaint Image")
if not path: return
# Create and initialize selected image
orig = bitmaps.BaseBitmap()
if orig.InitWith(path)[0] != c4d.IMAGERESULT_OK:
gui.MessageDialog("Cannot load image \"" + path + "\".")
return
# Get selected image infos
width, height = orig.GetSize()
bits = orig.GetBt()
# Create the copy and initialize it
copy = bitmaps.BaseBitmap()
copy.Init(width, height, bits)
multibmp = bitmaps.MultipassBitmap.AllocWrapper(copy)
layer_a = multibmp.AddLayer(multibmp.GetLayers(c4d.MPB_GETLAYERS_0),1)
# ColorPalette Variable vorbereiten
ColPal = [0,0]
cc = 0 # Zähler auf 0
while cc < 512: # ColorPalette 512 Einträge mit 0 füllen
ColPal.append(0)
cc = cc + 1
wy = 0
while wy < height: # Bildzeilen durchlaufen
wx = 0
while wx < width: # Bildpunkte der Zeile durchlaufen
readcol = orig.GetPixel(wx,wy) # Pixel als Farb-Vector auslesen
farbanzahl = 6 # In wie viele Farben aufteilen?
farbdivid = 256 * 3 / farbanzahl # Bit + Anzahl Farbkanäle geteilt durch die Farbanzahl = Divisor
# Farbkanäle runter rechnen
r=int(readcol[0]/farbdivid)
g=int(readcol[1]/farbdivid)
b=int(readcol[2]/farbdivid)
# reduzierte Farben in neue Bitmap eintragen
copy.SetPixel(wx,wy,r*farbdivid,g*farbdivid,b*farbdivid)
multibmp.SetPixel(wx,wy,r*farbdivid,g*farbdivid,b*farbdivid)
layer_a.SetPixel(wx,wy,r*farbdivid,g*farbdivid,b*farbdivid)
ColPal[r*g*b] = [r,g,b] # ColorPalette (512 mögliche Farben) mit Wert füllen
wx = wx +1 # Zeile +1
wy = wy + 1 # Spalte +1
bitmaps.ShowBitmap(orig) # Show original
bitmaps.ShowBitmap(copy) # Show copied image
bitmaps.ShowBitmap(multibmp) # Show copied image
# Einträge auswerten und anzeigen
ccc = 0
for element in ColPal:
if element > [0,0,0]:
print element
ccc = ccc + 1
print ccc
print layer_a
print multibmp
if __name__=='__main__':
main()
This is my code for loading an image.
Reduce the colors and write to a new bitmap.
And I create a multipass bitmap for test to create a layer.
Maybe, later I want to use OpenCV or PIL library for better convert the image.
Bernd