viewer.py 897 B

1234567891011121314151617181920212223242526272829
  1. from io import BytesIO
  2. import base64
  3. import streamlit as st
  4. def show_image_with_scroll(
  5. image,
  6. caption="",
  7. viewport_width=800,
  8. viewport_height=1200,
  9. zoom=1.0
  10. ):
  11. buf = BytesIO()
  12. image.save(buf, format="PNG")
  13. img_base64 = base64.b64encode(buf.getvalue()).decode("utf-8")
  14. width = image.width
  15. height = image.height
  16. html = f"""
  17. <div style="border:1px solid #e0e0e0;border-radius:6px;width:{viewport_width}px;height:{viewport_height}px;
  18. overflow:auto;background:#fff;margin-bottom:0.5rem;">
  19. <div style="transform:scale({zoom});transform-origin:top left;width:{width}px;height:{height}px;">
  20. <img src="data:image/png;base64,{img_base64}" style="width:{width}px;max-width:none;display:block;" />
  21. </div>
  22. </div>
  23. """
  24. st.markdown(html, unsafe_allow_html=True)
  25. if caption:
  26. st.caption(caption)