forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Screenshot without margins #7
Open
melonora
wants to merge
86
commits into
main
Choose a base branch
from
screenshot_without_margins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+568
−53
Open
Changes from all commits
Commits
Show all changes
86 commits
Select commit
Hold shift + click to select a range
6873d6c
remove 2d black margins screenshot
melonora 5948051
add param to viewer.screenshot
melonora d5c0c72
Merge branch 'main' into screenshot_without_margins
melonora 4cc0d90
fix 1 pixel off
melonora 6364fd0
Co-Authored-By: [email protected]
melonora fed5a73
Merge branch 'screenshot_without_margins' of https://github.com/melon…
melonora d3831bd
add test
melonora d012489
fix tests
melonora 4d9b73f
chance parameter name
melonora 9212a9f
fix tests
melonora d091e32
rename to no_margins
melonora 9daa6cb
switch to margins parameter and default old behaviour
melonora feae403
Merge branch 'main' into screenshot_without_margins
melonora 221692c
revert to fit_to_data
melonora 41a8326
disallow fit_to_data if canvas_only is False
melonora 10e86de
adjust test
melonora bffcbb2
fix test camera center
melonora e0e2498
close viewer prevent dangling animation
melonora effce1e
add fit_to_data to nbscreenshot
melonora 3a79f08
Update napari/components/viewer_model.py
melonora 0997e89
change parameter
melonora 33a2a32
update scale_factor calc
melonora 4bb7854
Update napari/_qt/qt_main_window.py
melonora e2ebf1b
update error messages
melonora 31e8e9a
Merge branch 'screenshot_without_margins' of https://github.com/melon…
melonora 2f71728
update docstrings
melonora 6fcf9be
fix error
melonora 7635530
minor grammar fix + fit docstring in 80c
jni 65729bb
Make error strings fit in 80c
jni 1d3140c
Update docstring for screenshot
jni 5d50d64
Fix outdated docstring for Viewer.reset_view
jni 8e5cba5
Fix fit-to-data docstring in two more places
jni 7eabe99
Fix test error message match
jni 55d6db9
rename parameter
melonora 7105658
change to export_view
melonora 6afe24b
address comments
melonora f622097
remove docstring
melonora df0b991
Merge branch 'main' into screenshot_without_margins
melonora cb1e951
fix test
melonora 6984781
Merge branch 'main' into screenshot_without_margins
melonora 21feada
revert to FutureWarning
melonora b840574
Merge branch 'screenshot_without_margins' of https://github.com/melon…
melonora 9fa60ca
change to export_figure
melonora 933cb7f
Merge branch 'main' into screenshot_without_margins
melonora 4b6c429
Match docstring formatting to PEP257 and clarify scale
jni 21f03cb
Update napari/_qt/_tests/test_qt_viewer.py
melonora a2eee46
adjust docstring
melonora 90ca6d3
typehints
melonora 657eaac
typehints
melonora 5cb8a55
typehints
melonora a80e64f
typehints
melonora b0eed32
Update napari/components/viewer_model.py
melonora c142c3c
add example
melonora 6e2a16c
Merge branch 'screenshot_without_margins' of https://github.com/melon…
melonora 5cccde9
set default scale to 1, adjust docstring
melonora fbe7c58
adjust example and docstrings
melonora 74aa262
adjust explanation docstring
melonora e28e9d3
Only allow float or int
melonora b511309
fix isinstance
melonora 32f9bb3
Merge branch 'main' into screenshot_without_margins
jni 3b1fad7
Update napari/_qt/qt_main_window.py
melonora 343182a
Apply suggestions from code review
melonora 04a2ba2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 667795f
add missed import
Czaki 9137b27
Update napari/_qt/_tests/test_qt_viewer.py
Czaki 25167dc
Update napari/viewer.py
melonora 57a93df
use extent.step
melonora 8cb09bf
Merge branch 'screenshot_without_margins' of https://github.com/melon…
melonora f6ff4bf
remove conditional
melonora dde2642
adjust docstring
melonora 25596a8
adjust docstring
melonora 9b1a230
scale -> scale_factor in gallery example
jni eb89264
Add some missing layer actions tests (split rgb, split and merge acti…
dalthviz 7646145
Restore events to `QtViewer.canvas` (#7060)
Czaki 4296ac5
Use minimum step size across all dims
jni e8c3f6f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f5fe4f4
Remove f-string from translation call
jni 374a6df
Move `IO_Utilities` and `Acquire` submenus to their own group in the …
DragaDoncila 46fdf4c
Add empty menu placeholder actions using functional context keys from…
DragaDoncila bc1d95f
Add warning in docstring about ignored size
jni 2bd587d
Minor docstring clarification
jni 4d06794
Fix incorrect setting of scale in test
jni 29f6d66
Use allclose to test screenshot size when rounding
jni de4bb80
Refactor screenshot function to clarify logic flow
jni 77bf7ed
Docstring and typing fixes
jni 2935b04
Merge branch 'main' into screenshot_without_margins
jni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Export Figure | ||
============= | ||
|
||
Display one shapes layer ontop of one image layer using the ``add_shapes`` and | ||
``add_image`` APIs. When the window is closed it will print the coordinates of | ||
your shapes. | ||
|
||
.. tags:: visualization-advanced | ||
""" | ||
|
||
import numpy as np | ||
from skimage import data | ||
|
||
import napari | ||
|
||
# create the viewer and window | ||
viewer = napari.Viewer() | ||
|
||
# add the image | ||
img_layer = viewer.add_image(data.camera(), name='photographer') | ||
img_layer.colormap = 'gray' | ||
|
||
# create a list of polygons | ||
polygons = [ | ||
np.array([[11, 13], [111, 113], [22, 246]]), | ||
np.array( | ||
[ | ||
[505, 60], | ||
[402, 71], | ||
[383, 42], | ||
[251, 95], | ||
[212, 59], | ||
[131, 137], | ||
[126, 187], | ||
[191, 204], | ||
[171, 248], | ||
[211, 260], | ||
[273, 243], | ||
[264, 225], | ||
[430, 173], | ||
[512, 160], | ||
] | ||
), | ||
np.array( | ||
[ | ||
[310, 382], | ||
[229, 381], | ||
[209, 401], | ||
[221, 411], | ||
[258, 411], | ||
[300, 412], | ||
[306, 435], | ||
[268, 434], | ||
[265, 454], | ||
[298, 461], | ||
[307, 461], | ||
[307, 507], | ||
[349, 510], | ||
[352, 369], | ||
[330, 366], | ||
[330, 366], | ||
] | ||
), | ||
] | ||
|
||
# add polygons | ||
layer = viewer.add_shapes( | ||
polygons, | ||
shape_type='polygon', | ||
edge_width=1, | ||
edge_color='coral', | ||
face_color='royalblue', | ||
name='shapes', | ||
) | ||
|
||
# add an ellipse to the layer | ||
ellipse = np.array([[59, 222], [110, 289], [170, 243], [119, 176]]) | ||
layer.add( | ||
ellipse, | ||
shape_type='ellipse', | ||
edge_width=5, | ||
edge_color='coral', | ||
face_color='purple', | ||
) | ||
|
||
labels = layer.to_labels([512, 512]) | ||
labels_layer = viewer.add_labels(labels, name='labels') | ||
|
||
points = np.array([[100, 100], [200, 200], [333, 111]]) | ||
size = np.array([10, 20, 20]) | ||
viewer.add_points(points, size=size) | ||
|
||
# Export figure and change theme before and after exporting to show that the background canvas margins | ||
# are not in the exported figure. | ||
viewer.theme = "light" | ||
# Optionally for saving the exported figure: viewer.export_figure(path="export_figure.png") | ||
export_figure = viewer.export_figure() | ||
scaled_export_figure = viewer.export_figure(scale_factor=5) | ||
viewer.theme = "dark" | ||
|
||
viewer.add_image(export_figure, rgb=True, name='exported_figure') | ||
viewer.add_image(scaled_export_figure, rgb=True, name='scaled_exported_figure') | ||
viewer.reset_view() | ||
|
||
# from skimage.io import imsave | ||
# imsave('screenshot.png', screenshot) | ||
|
||
if __name__ == '__main__': | ||
napari.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
from typing import Union | ||
|
||
from app_model.expressions import parse_expression | ||
from app_model.types import Action, MenuItem, SubmenuItem | ||
|
||
from napari._app_model import get_app | ||
from napari._app_model.constants import MenuGroup, MenuId | ||
|
||
MenuOrSubmenu = Union[MenuItem, SubmenuItem] | ||
|
||
|
||
def contains_dummy_action(menu_items: list[MenuOrSubmenu]) -> bool: | ||
"""True if one of the menu_items is the dummy action, otherwise False. | ||
|
||
Parameters | ||
---------- | ||
menu_items : list[MenuOrSubmenu] | ||
menu items belonging to a given menu | ||
|
||
Returns | ||
------- | ||
bool | ||
True if menu_items contains dummy item otherwise false | ||
""" | ||
for item in menu_items: | ||
if hasattr(item, 'command') and 'empty_dummy' in item.command.id: | ||
return True | ||
return False | ||
|
||
|
||
def is_empty_menu(menu_id: str) -> bool: | ||
"""Return True if the given menu_id is empty, otherwise False | ||
|
||
Parameters | ||
---------- | ||
menu_id : str | ||
id of the menu to check | ||
|
||
Returns | ||
------- | ||
bool | ||
True if the given menu_id is empty, otherwise False | ||
""" | ||
app = get_app() | ||
if menu_id not in app.menus: | ||
return True | ||
if len(app.menus.get_menu(menu_id)) == 0: | ||
return True | ||
if len(app.menus.get_menu(menu_id)) == 1 and contains_dummy_action( | ||
app.menus.get_menu(menu_id) | ||
): | ||
return True | ||
return False | ||
|
||
|
||
def no_op() -> None: | ||
"""Fully qualified no-op to use for dummy actions.""" | ||
|
||
|
||
def get_dummy_action(menu_id: MenuId) -> tuple[Action, str]: | ||
"""Return a dummy action to be used for the given menu. | ||
|
||
The part of the menu_id after the final `/` will form | ||
a unique id_key used for the action ID and the when | ||
expression context key. | ||
|
||
Parameters | ||
---------- | ||
menu_id: MenuId | ||
id of the menu to add the dummy action to | ||
|
||
Returns | ||
------- | ||
tuple[Action, str] | ||
dummy action and the `when` expression context key | ||
""" | ||
# NOTE: this assumes the final word of each contributable | ||
# menu path is unique, otherwise, we will clash. Once we | ||
# move to using short menu keys, the key itself will be used | ||
# here and this will no longer be a concern. | ||
id_key = menu_id.split('/')[-1] | ||
action = Action( | ||
id=f'napari.{id_key}.empty_dummy', | ||
title='Empty', | ||
callback=no_op, | ||
menus=[ | ||
{ | ||
'id': menu_id, | ||
'group': MenuGroup.NAVIGATION, | ||
'when': parse_expression(context_key := f'{id_key}_empty'), | ||
} | ||
], | ||
enablement=False, | ||
) | ||
return action, context_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only fails when running the test itself. If you use the same code in a python script it does not fail. For some reason, there is no response to the canvas resize event when running inside a test.