You skip important information:
A) what does the console say? (You added a print but never show the outcome)
B) what is Image? You need to provide your imports even on snippets
Create texture source
The API says:
class bge.texture.ImageBuff(width, height, color=0, scale=False)
Image source from image buffer.
width (int) – Width of the image.
height (int) – Height of the image.
color (int in [0, 255]) – Value to initialize RGB channels with. The initialized buffer will have all pixels set to (color, color, color, 255). (optional)
scale (bool) – Image uses scaling. (optional)
Somehow they forgot to tell that all parameters are optional and not type checked.
Therefore your code:
is equal to
And creates a black buffer with zero content.
–> results in a black texture
You can initialize the ImageBuf e.g. with a gray color:
source = texture.ImageBuff(1, 1, 150)
Now you know how you can generate a gray texture.
With the grey initial texture you know the source was successfully created and displayed.
Now the interesting part.
Create a custom buffer
You create a buffer with pixel information. You can use an array. The size equals: widthheightcolor-size (=3)
The color-size can vary dependent on the applied filter.
def generateColorRampPattern(width, height):
buffer = array.array('B')
for x in range(width):
for y in range(height):
red = int(256*x/width))
green = int(256*y/height))
You load this buffer into the above imageBuff overriding whatever you initialized before.
width = 10
height = 10
buffer = generateColorRampPattern(width, height)
source.load(buffer, width, height)
This allows you to generate your very own colorful texture.
Buffer from image file
I can’t tell much about this. I recommend to look at attribute bge.texture.ImageFFmpeg.image.
Isn’t that what you want?
You get your image as buffer -> you can manipulate the buffer -> you can apply the buffer to the texture source.
There might be another way to directly load an image file into a buffer. I can’t tell right now.
You followed the example provided by the API Documentation.
I do not think that module logic is a good place to store a texture.
- You restrict yourself to a single texture
- You save it scene independent
- Great danger to override the texture with another one
This will not be a big deal on static texture as you do not even need to store them. Videofiles and Camera needs constant updates.
I suggest you either create a sufficient container (list, dict), or store it somewhere else. A good place is one of the game objects that use this texture.
owner["texture"] = object_texture
This way the logic and the storage are close to each other. It enables you to use exactly the same code with different objects and different materials. A little customization (e.g. filenames) can be done via external properties while you keep the texture in an internal property.