Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scene.save_image backround alpha always 255 #2302

Open
Houston256 opened this issue Oct 16, 2024 · 2 comments
Open

scene.save_image backround alpha always 255 #2302

Houston256 opened this issue Oct 16, 2024 · 2 comments

Comments

@Houston256
Copy link

Houston256 commented Oct 16, 2024

Issue description:

The alpha channel is always set to 255, even if I specify backround to be [... , 4].

Minimal reproducible example:

import trimesh
import numpy as np
import PIL.Image as Image

mesh = trimesh.creation.cone(radius=1, height=1)

scene = trimesh.Scene([mesh])

scene.set_camera(resolution=(100, 100))

image_data = scene.save_image(background=[1, 2, 3, 4])  # alpha value of backgound should be 4

image = Image.open(io.BytesIO(image_data))

image = np.array(image)

print(np.unique(image[..., 3])) # shows [255] instead of [4]

System:

I'm running this on WSL2 / Ubuntu 22.04.5 LTS.
Python version 3.10.12

requirements.txt:

numpy
pillow
trimesh[all]

My setup:

sudo apt-get update
sudo apt-get install freeglut3 freeglut3-dev libglu1-mesa-dev

pip install -r requirements.txt (inside venv)

image

@brandonrwin
Copy link

Can't reproduce with:

# Python 3.10.11
trimesh==4.5.1
pillow==10.4.0
numpy==1.26.4

Using (screenshots bad, text good):

import trimesh
import numpy as np
import io
from PIL import Image

mesh = trimesh.creation.cone(radius=1, height=1)
scene = trimesh.Scene([mesh])
scene.set_camera(resolution=(100, 100))
image_data = scene.save_image(background=[1, 2, 3, 4])
with open('scene.png', 'wb') as f:
    f.write(image_data)
image = Image.open(io.BytesIO(image_data))
image = np.array(image)
print(np.unique(image[..., 3]))

I get [ 4 67 129 192 255], and confirmed that "scene.png" has a transparent background, in an image editor.

@SamuelBoerlin
Copy link

I've also run into this and could fix it by setting alpha_size=8 in the pyglet config like so e.g.:

from pyglet import gl
scene.save_image(..., window_conf=gl.Config(sample_buffers=1, samples=4, depth_size=24, double_buffer=True, alpha_size=8))

It seems that it defaults to alpha_size=0 in my headless environment, which of course prevents the alpha channel from working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants