question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Public access to canvas size

See original GitHub issue

🚀 Feature

There does not seem to be a public way to access the canvas size (alternatively the shape of the displayed/rendered image). viewer._canvas_size is private and also viewer.window.qt_viewer.canvas.size now gives a FutureWarning and will break all such use cases as soon as 0.5.0 is released.

Motivation

The property viewer.camera.zoom is (still) public and connects the size of canvas pixels with the size of world pixels. Since we usually work a lot in the world and data spaces with world pixels, we would also need some information about the canvas space for being able to set a reasonable zoom factor.

Pitch

Provide a public API for reading the current rendered view’s size (width, height).

Additional context

Example use case (simplified):

from napari import Viewer
from napari.layers import Layer
import numpy as np

def zoom_layer_extents(viewer: Viewer, layer: Layer):
    dim_indices = [d - viewer.dims.ndim for d in viewer.dims.displayed]
    if np.max(np.abs(dim_indices)) <= layer.ndim:
        extent = layer.extent.world[:, dim_indices]
        canvas_size = np.asarray(viewer.window.qt_viewer.canvas.size)
        layers_size = np.flip(np.diff(extent, axis=0))
        size_ratios = canvas_size / layers_size
        zoom = np.min(size_ratios)
        viewer.camera.zoom = zoom
        viewer.camera.center = np.mean(extent, axis=0)

viewer = Viewer(show=True)
layer1 = viewer.add_image(np.random.random((200, 100)))
layer2 = viewer.add_image(np.random.random((100, 200)))
zoom_layer_extents(viewer, layer2)
FutureWarning: Public access to Window.qt_viewer is deprecated and will be removed in
v0.5.0. It is considered an "implementation detail" of the napari
application, not part of the napari viewer model. If your use case
requires access to qt_viewer, please open an issue to discuss.
  viewer.window.qt_viewer.canvas.size

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jnicommented, Feb 25, 2022

I think let’s just make viewer.canvases a list from the beginning, containing one canvas initially. But yes, I need canvas size also in my plugin 😂 So we are 100% on board @aeisenbarth!

1reaction
Czakicommented, Mar 3, 2022

you may also would like to trace #3781, which, I have hope,should be implemented before 0.5.0 and which should allow to set viewing area instead of zoom calculation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Storage in Canvas
Media Files: Canvas converts video and audio files up to 500 MB through the Media tool. If a file exceeds the 500 MB...
Read more >
Class size limit for free for teacher tier? - Canvas Community
I'm thinking about using the Canvas Free Teacher account to create a course, but I can't figure out if there is a max...
Read more >
About lino canvas
Upload a desired background image for the canvas. The canvas size is 2560×1920 pixels, and if you upload a smaller size image, the...
Read more >
Creating and Arranging Course Content
What are the file size limits in Canvas? · Course site: A Canvas course site can contain 10 gigabytes (GB) of data. ·...
Read more >
How to upload personal picture files into Canvas: | Taft College
You will then have access to any images you uploaded to your account. * Tip: Dimensions around this (360 x 640 for portrait...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found