yt icon indicating copy to clipboard operation
yt copied to clipboard

BUG: cannot set image background color

Open neutrinoceros opened this issue 2 years ago • 1 comments

Bug report

Bug summary This section of our docs clearly shows that the functionality it's supposed to demonstrate currently doesn't work https://yt-project.org/doc/cookbook/simple_plots.html#image-background-colors

neutrinoceros avatar Mar 20 '22 14:03 neutrinoceros

Took a quick look, not sure what the fix might be, but here are some notes:

im.write_png works as expected. The following (from the docstring of write_png) works:

from yt.data_objects.image_array import ImageArray
import numpy as np

im = np.zeros([64, 128, 4])
for i in range(im.shape[0]):
    for k in range(im.shape[2]):
        im[i, :, k] = np.linspace(0.0, 10.0 * k, im.shape[1])

im_arr = ImageArray(im)
im_arr.write_png("standard.png")
im_arr.write_png("non-scaled.png", rescale=False)
im_arr.write_png("black_bg.png", background="black")
im_arr.write_png("white_bg.png", background="white")
im_arr.write_png("green_bg.png", background=[0, 1, 0, 1])
im_arr.write_png("transparent_bg.png", background=None)

The issue seems to be that VolumeSource.finalize_image will set the alpha channel to 1. Because ImageArray.add_background_color blends the background color with the image based on the alpha channel, the background color will have no effect. Here's finalize_image where it happens:

https://github.com/yt-project/yt/blob/32d923cca3dff5cc09e79b2925b7767026d9f91e/yt/visualization/volume_rendering/render_source.py#L480-L495

I tried flipping that grey_opacity flag and it didn't turn out well as the background color will be blended in everywhere. e.g.,:

import yt
ds = yt.load("Enzo_64/DD0043/data0043")
sc = yt.create_scene(ds, field=("gas", "density"))
sc.sources['source_00'].transfer_function.grey_opacity = True
im = sc.render()
_ = im.write_png("vr_black_bg.png", background="white", sigma_clip=8.0)

results in

vr_white_bg

chrishavlin avatar Jun 23 '22 21:06 chrishavlin

Duplicate of #1579?

Socob avatar Sep 19 '23 06:09 Socob

Absolutely, thanks for pointing it out !

neutrinoceros avatar Sep 19 '23 07:09 neutrinoceros