Skip to main content

A Streamlit Component to display an image with interactive zoom functionality.

Project description

Image Zoom - Streamlit Component

This repository presents a Python Streamlit component that wraps HTML, CSS, and JS code, enabling the creation of an interactive image zoom application. Try online demo!

Installation

  • Install via pip:
pip install streamlit
pip install streamlit-image-zoom
  • Install via git clone:
git clone https://github.com/vgilabert94/streamlit-image-zoom

Documentation

Main function

def image_zoom(image: Union[Image.Image, np.ndarray],
                mode: Optional[str] = "default",
                size: Optional[Union[int, Tuple[int, int]]] = 512,
                keep_aspect_ratio: Optional[bool] = True,
                keep_resolution: Optional[bool] = False,
                zoom_factor: Optional[Union[float, int]] = 2.0,
                increment: Optional[float] = 0.2,
            ) -> HTML:

    return component

Parameters

  • image: The image to be displayed. It can be either a PIL Image or a NumPy array.
  • mode: The mode of interaction for zooming. Valid options are "default" (zoom on mousemove), "mousemove" (zoom on mousemove), "scroll" (zoom on scroll), "both" (zoom on both mousemove and scroll) or "dragmove" (Single-click to zoom in, click and drag to move the zoomed image). Default is "default".
  • size: The desired size of the displayed image. If an integer is provided, the image will be resized to have that size (width = height). If a tuple of integers (width, height) is provided, the image will be resized to fit within the specified dimensions while maintaining its aspect ratio. Default is 512.
  • keep_aspect_ratio: Whether to maintain the aspect ratio of the image during resizing. If True, the image will be resized while preserving its aspect ratio. If False, the image will be resized to exactly match the provided size without preserving aspect ratio. Default is True.
  • keep_resolution: Whether to keep the original resolution for zooming. If True, use the original resolution for zooming. If False, use the resized image for zooming. Default is False.
    Note: Setting this parameter to True may result in slower performance, especially for images with large sizes.
  • zoom_factor: The zoom factor applied to the image when zooming in. Default is 2.0.
  • increment: The increment value for adjusting the zoom level when scrolling. Should be between 0 and 1. Default is 0.2.

Modes

Here's a list explaining the operation of each mode in the app:

mousemove or default

As you move the mouse, the zoom level adjusts accordingly, providing a dynamic zoom experience.

scroll

Zooming is activated exclusively by scrolling (wheel).

both

This mode combines the functionalities of both "mousemove" and "scroll" modes, allowing users to navigate the image with a fixed zoom level. Zooming is activated by scrolling (using the mouse wheel) and moving the mouse cursor, offering flexibility in how users interact with the zoom feature.

dragmove

Drag and move mode Single-click to zoom in, click and drag to move the zoomed image, and double-click to zoom out.

Examples

import streamlit as st
from streamlit_image_zoom import image_zoom
from PIL import Image
import cv2

# Supported Image Formats
# PIL image
from PIL import Image
image = Image.open("images/building.jpg")

# Numpy array (opencv, scikit-image, etc)
import cv2
image = cv2.cvtColor(cv2.imread("image.jpg"), cv2.COLOR_BGR2RGB)

# Display image with default settings
image_zoom(image)

# Display image with custom settings
image_zoom(image, mode="scroll", size=(800, 600), keep_aspect_ratio=False, zoom_factor=4.0, increment=0.2)

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Vicent Gilabert

Linkedin

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_image_zoom_with_caption-0.0.7.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file streamlit_image_zoom_with_caption-0.0.7.tar.gz.

File metadata

File hashes

Hashes for streamlit_image_zoom_with_caption-0.0.7.tar.gz
Algorithm Hash digest
SHA256 2c74940b505e97a528dde41898dc64883816f0724a2cfd66ae66a5e761cc25cc
MD5 b41f5564e088b3f41499a8ba08d5d26d
BLAKE2b-256 f4ce1c400c1cf58ac5fe32b931680fbcb08843126673c2c71daf712904400c1b

See more details on using hashes here.

File details

Details for the file streamlit_image_zoom_with_caption-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_image_zoom_with_caption-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 324590db043cccbe329d193b1d538bb00efb1f55ac95ec186e3759609af59c2c
MD5 7a69d125525620cc7043d7e3a3d90281
BLAKE2b-256 2eb3d948496b95e94df7cb093d7f4d0d981d9f92a780f96ef68ad4162949ded0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page