mikedh / trimesh

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

Retina Mac save_image doubles the image resolution everytime #1204

Open cansik opened 3 years ago

cansik commented 3 years ago

Problem

When rendering a view with save_image() the resulting image is twice the size of the camera resolution on MacOS (10.15.7) using trimesh 3.9.10. I first thought this is due to it's hdpi rendering, but after the second rendering, the size has doubled again 🤷🏻‍♂️. The same problem could not be verified on a Ubuntu 20.04 & Windows10. Btw. also the scene.show() renders in the higher resolution.

Example Code

Here an example code to reproduce it:

import io
from math import radians

import trimesh
from PIL import Image

mesh = trimesh.load("bunny.ply")

# setup scene
scene = mesh.scene()
scene.camera.resolution = [512, 512]
scene.camera.fov = 50 * (scene.camera.resolution /
                         scene.camera.resolution.max())

# create png 1
data = scene.save_image(visible=True)
png = Image.open(io.BytesIO(data))

print(png.size)

# create png 2
data = scene.save_image(visible=True)
png = Image.open(io.BytesIO(data))

print(png.size)

scene.show()

Results

(1024, 1024)
(2048, 2014)

It should be (512, 512) all the time. Is there maybe a state set which should not be?

cansik commented 3 years ago

A workaround is to explicit set the resolution parameter in save_image():

w = 800
h = 500

data = scene.save_image(resolution=[w, h], visible=True)

This still doubles the resolution, but I guess this is due to the HDPI screen.