我遇到了同样的问题,但无法使用ipywidgets(笔记本最终通过fastpages转换为html)。
我通过创建一个包含所有图像的xarray,然后使用plotly.js动画功能,找到了一种可行的方法。它添加了一个滑块,可以轻松切换图像。这是一个例子:
import numpy as np
import plotly.express as px
from imageio import imread
import xarray as xr
def show_images_carousel(images, labels, key: str, title: str, height:int):
stacked = np.stack(images, axis=0)
xrData = xr.DataArray(
data = stacked,
dims = [key, 'row', 'col', 'rgb'],
coords = {key: labels}
)
layout_dict = dict(yaxis_visible=False, yaxis_showticklabels=False, xaxis_visible=False, xaxis_showticklabels=False)
return px.imshow(xrData, title=title, animation_frame=key).update_layout(layout_dict)
def show_images_carousel_from_urls(image_urls, labels, key: str, title:str, height:int):
images = [imread(url, pilmode='RGB') for url in image_urls]
return show_images_carousel(images, labels, key, title, height)
使用方法如下:
images = {
"simulation_images/rgbspan.png": 'Original',
"simulation_images/rgbspan_protan_brettel1997.png": "Brettel 1997",
"simulation_images/rgbspan_protan_vienot1999.png": "Viénot 1999",
"simulation_images/rgbspan_protan_machado2009.png": 'Machado 2009',
"simulation_images/rgbspan_protan_coblisv1.png": "Coblis V1",
"simulation_images/rgbspan_protan_coblisv2.png": "Coblis V2"
}
fig = show_images_carousel_from_urls (images.keys(), list(images.values()), 'Method', None, 700)
fig.show()
![Example of the output](https://istack.dev59.com/SZz4Z.webp)