You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按Bokeh文档设置仍无法导出透明背景PNG或获取Pillow图像,求排查

Bokeh导出透明背景PNG的问题解决

问题根源

你按照Bokeh文档设置plot.background_fill_color = None和plot.border_fill_color = None后,导出的PNG还是白色背景,核心原因是WebDriver的截图会捕获浏览器的默认背景:

  • Bokeh的设置仅让绘图区域本身透明,但浏览器的默认页面背景是白色,get_screenshot_as_png()会把这个底层白色背景一起捕捉进去,最终输出的PNG自然带有白色底色。

解决方法

方法1:修改WebDriver的页面背景为透明

在创建WebDriver时,强制设置浏览器页面的背景为透明,这样截图时底层背景就是透明的,配合Bokeh的绘图透明设置就能得到纯透明的PNG。以Chrome为例:

from bokeh.io import export_png
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 配置Chrome选项,设置页面背景为完全透明
chrome_options = Options()
# 8位十六进制值:前两位是alpha通道(00=完全透明),后六位是RGB(000000=黑色,不影响最终透明效果)
chrome_options.add_argument("--background-color=00000000")

# 初始化驱动
driver = webdriver.Chrome(options=chrome_options)

# 配置你的Bokeh绘图
plot.background_fill_color = None
plot.border_fill_color = None

# 导出PNG,使用自定义驱动
export_png(plot, filename="transparent_plot.png", driver=driver)

# 关闭驱动
driver.quit()

注意:不同浏览器的背景设置参数可能不同,Firefox可通过自定义CSS或修改配置项实现类似效果。

方法2:用Pillow后期将白色背景转为透明

如果修改WebDriver设置不生效,可以用Pillow对导出的PNG进行后处理,把白色背景替换为透明:

from PIL import Image

# 读取导出的带白色背景的PNG
img = Image.open("white_bg_plot.png").convert("RGBA")
pixel_data = img.getdata()

# 遍历像素,将白色(可根据实际情况调整容差)转为透明
transparent_data = []
for pixel in pixel_data:
    # 精确匹配白色(255,255,255),如果是偏白的颜色可以调整阈值,比如pixel[0]>245 and pixel[1]>245 and pixel[2]>245
    if pixel[:3] == (255, 255, 255):
        transparent_data.append((255, 255, 255, 0))  # alpha设为0即透明
    else:
        transparent_data.append(pixel)

# 更新图片并保存
img.putdata(transparent_data)
img.save("transparent_plot.png", "PNG")

获取带透明背景的Pillow图像对象

如果不需要保存文件,想直接得到Pillow对象,可以在截图后直接加载处理:

from PIL import Image
import io
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bokeh.io.export import get_layout_html

# 配置驱动背景透明
chrome_options = Options()
chrome_options.add_argument("--background-color=00000000")
driver = webdriver.Chrome(options=chrome_options)

# 渲染Bokeh绘图到驱动
html = get_layout_html(plot)
driver.get("data:text/html;charset=utf-8," + html)

# 捕获截图并转为Pillow对象
png_bytes = driver.get_screenshot_as_png()
transparent_img = Image.open(io.BytesIO(png_bytes)).convert("RGBA")

# 后续可直接操作transparent_img,比如调整或保存
driver.quit()

内容的提问来源于stack exchange,提问作者TMS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:35:38