Skip to content

How would it be possible to create an image cropper #4414

Closed Answered by falkoschindler
siren15 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @siren15,

I'm not familiar with cropperjs, so it's hard to tell how easily you can integrate it with a NiceGUI app.

But using our ui.interactive_image you can build an image crop tool rather quickly:

from PIL import Image
from nicegui import events, ui

IMAGE_PATH = 'website/static/logo.png'

start: dict[str, float] = {}

def handle_mouse(e: events.MouseEventArguments) -> None:
    if e.type == 'mousedown':
        start.update(x=e.image_x, y=e.image_y)
    elif start:
        top = min(start['x'], e.image_x)
        left = min(start['y'], e.image_y)
        width = abs(start['x'] - e.image_x)
        height = abs(start['y'] - e.image_y)
        img.content = f'<rect x={top} y={left} w…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@siren15
Comment options

Answer selected by siren15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants