mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3.01k stars 582 forks source link

scene.save_image backround alpha always 255 #2302

Open Houston256 opened 1 month ago

Houston256 commented 1 month ago

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 commented 1 week ago

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.