Customize napari file menu

I want to be able to add custom functionality to the open folder file menu option in napari, is there currently a way to modify the file menu like this? Looks like this PR may be relevant https://github.com/napari/napari/pull/1800/.

if you want to change the behavior of what happens when someone opens a folder in napari, then what you’ll probably want to do is write a napari IO plugin implementing napari_get_reader. This is what gets called behind the scenes when someone tries to open a folder in napari.

If you’re specifically talking about adding a new item to the File Menu itself… then #1800 isn’t what you need, rather, you’ll need to add a QAction to the menu like so:

import napari
from qtpy.QtWidgets import QAction

def some_callback():
    """function to call when your menu item is picked."""
    print("hi!")

viewer = napari.Viewer()
action = QAction('Your Item Name', viewer.window._qt_window)
action.triggered.connect(some_callback)
viewer.window.file_menu.addAction(action)

If you’re not just trying to do something for yourself, but looking to create a plugin that other people could install and use, then that’s not something that’s currently possible. feel free to expand on what you’re trying to do.

thanks for the help. Actually was just thinking that i wouldn’t have to customize the open folder action like i wanted to do initially if instead i could provide my own callback when cycling through the images that are in the folder that i just opened?

Here’s an example of what i would like to do:
for each image in the folder, take the image and compute a binary mask, add a new layer to the viewer of the binary image.

Currently i’m a little confused on how to access the images in the folder along with how to customize an action when a user is cycling through the images in an opened folder.

Sorry if this is becoming off topic from my original question, would it be better if i created a new issue and closed this one?

This could be done with a custom reader plugin that loaded each image and returned either just the binary mask or both the binary mask and the image as @talley suggests.

You could end up with something where someone dragged and dropped the folder of images into napari, your reader plugin would get called and you’d end up with a stack of both raw and binarized images in two napari layers.

The link from above on writing IO plugins is probably the best place to start, let us know if you have additional questions!

1 Like