jupyter-widgets-contrib / ipycanvas

Interactive Canvas in Jupyter
https://ipycanvas.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
685 stars 62 forks source link

add width, height support to put_image_data #304

Open orena1 opened 1 year ago

orena1 commented 1 year ago

Hi, As far as I can see, there is no way to scale an image when using put_image_data. e.g.:

from ipycanvas import Canvas
import numpy as np
canvas = Canvas(width=300, height=300)
canvas.put_image_data(np.random.uniform(size=(200,200,3)))
canvas

image

No matter the width and height of the canvas... Which is different from draw_image where there are two other variables (width, height) which scale the image, is it possible to add this feature?

martinRenou commented 1 year ago

ipycanvas tries to stay close to the web canvas API, and this is not a feature that this API provides: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/putImageData

Though you can actually work around this by having another Canvas:

from ipycanvas import Canvas
import numpy as np

canvas1 = Canvas(width=200, height=200)
canvas1.put_image_data(np.random.uniform(size=(200,200,3)))

canvas2 = Canvas(width=300, height=300)
# Draw canvas1 on canvas2 and scale it down to (100, 100)
canvas2.draw_image(canvas1, 0, 0, 100, 100)

canvas2