ImageBuff: imageToArray

imageToArray(source, mode)
  • Returns a Video Texture Image Object

source:

  • A Video Texture Image source.
  • Types:  VideoFFmpeg
       ImageFFmpeg
       ImageBuff
       ImageMix
       ImageRender
       ImageMirror
       ImageViewport

mode:

  • the return pixel format of the Video Texture Image source.
  • Optional.  Default is "RGBA"
  • Type:  string
  • "RGB" = Red, Green and Blue color channels.
  • "RGBA" = Red, Green, Blue and Alpha color channels.
  • "RGB0" = 4 color channels.  Red, Green, Blue and Zero.  The number Zero sets all of the Alpha values to 0.
     
  • "RGB1" = 4 color channels.  Red, Green, Blue and One.  The number One sets all of the Alpha values to 255.
Note: Special Mode "F"
  • Return the image as a float array.
  • Used to retrieve the depth buffer of the ImageViewport and ImageRender object. 
  • Returns a one dimensional float array size (width*height).
Sample Code

###################### use imageToArray

#import bge
import bge

# get current scene
scene = bge.logic.getCurrentScene()

# get list of objects in scene
objList = scene.objects

# get object named Painting
obj = objList["Painting"]

# get matID for the obj named Painting
# I named the material Canvas
matID = bge.texture.materialID(obj, "MACanvas")

# get the 1st texture on object named Painting
painting_texture = bge.texture.Texture(obj, matID)

# get the name of the image
# I'm using an image named Mona_Lisa.jpg
imageName = "Mona_Lisa.jpg"

# get image path
# Mona_Lisa.jpg is in same directory as saved blend
imagePath = bge.logic.expandPath("//" + imageName)

# get image as an ImageFFmpeg
image = bge.texture.ImageFFmpeg(imagePath)

# load image into a bgl buffer
image_buffer = bge.texture.imageToArray(image, 'RGB')

# get image size
image_size = image.size

# get the texture
painting_texture = bge.texture.Texture(obj, matID)

# use ImageBuff as the source
painting_texture.source = bge.texture.ImageBuff()

# load the image from the buffer into ImageBuff
painting_texture.source.load(image_buffer, image_size[0], image_size[1])

# save as an object variable
obj['painting'] = painting_texture

# display the image
painting_texture.refresh(True)