Hi all,
I am starting to use dash and I want to build an application where I can compare two images using a slider. The effect is sometimes called before-after here is an example:
would you have any idea on how to implement this using dash?
The one you showed seems good, the only thing is that I would like to pass images as numpy arrays as opposed to pngs or jpgs like in here: html.Img | Dash for Python Documentation | Plotly
I was checking your code but it seems that to be simple it needs to exist a very close component otherwise it would be more difficult I suppose.
Yes, the closer to the react component, the easier. For your usecase, you could save the numpy arrays as images in a hosted folder, and set urls to these images as the properties of the before-after-slider.
wow this is really awesome! Thank you very much for sharing!
in your implementation is it possible to change the before and after images dynamically? like on a callback?
Thanks @Emil !
I am running it and it works out of the box
I modified a little so that the code will modify the images each time I press a button. My code looks like this:
import dash_html_components as html
import numpy as np
from dash import Dash
from dash_extensions import BeforeAfter
from PIL import Image
import dash
def make_image(label):
data = 255*np.random.random((width, height, 3))
data = data.astype(np.uint8)
# rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)
if label == 'after':
data[...] = (255*np.random.rand(3)).astype(np.uint8)
Image.fromarray(data).save(f'assets/{label}.png')
return f"assets/{label}.png"
width, height = 800, 600
app = Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Div(id='slider', children=[
BeforeAfter(before=make_image("before"), after=make_image(
"after"), width=width, height=height)]),
html.Div([html.Button('Submit', id='submit-val', n_clicks=0)])])
@app.callback(
dash.dependencies.Output('slider', 'children'),
dash.dependencies.Input('submit-val', 'n_clicks'))
def update_output(n_clicks):
out = BeforeAfter(before=make_image("before"), after=make_image(
"after"), width=width, height=height)
return out
if __name__ == '__main__':
app.run_server()
The code is working because I can see the images being modified; however they are not updated in the website. Am I doing something wrong?
Thanks again awesome contribution!