-
Notifications
You must be signed in to change notification settings - Fork 62
add imgui file dialog example #950
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
Open
FlynnOConnell
wants to merge
5
commits into
main
Choose a base branch
from
examples-imgui-filedialog
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.
+110
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1cd148
add imgui file dialog example
FlynnOConnell 6fda6c7
simplify to use image graphic
FlynnOConnell 812b0fe
Merge branch 'main' into examples-imgui-filedialog
kushalkolar bf84c57
Merge branch 'main' into examples-imgui-filedialog
FlynnOConnell 8eb42c0
check impage format and shape against buffer
FlynnOConnell 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 hidden or 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 @@ | ||
| """ | ||
| ImGui File Dialog | ||
| ================= | ||
|
|
||
| Example showing how to use imgui_bundle's portable file dialogs to load | ||
| and display image files using ImageGraphic. | ||
|
|
||
| Demonstrates: | ||
| - Creating a custom EdgeWindow GUI panel | ||
| - Using portable_file_dialogs for native file selection | ||
| - Loading images with imageio and displaying them | ||
| """ | ||
|
|
||
| # test_example = false | ||
| # sphinx_gallery_pygfx_docs = 'screenshot' | ||
|
|
||
| import imageio.v3 as iio | ||
|
|
||
| import fastplotlib as fpl | ||
| from fastplotlib.ui import EdgeWindow | ||
| from imgui_bundle import imgui, portable_file_dialogs as pfd | ||
|
|
||
|
|
||
| # load an initial image from imageio's built-in images | ||
| initial_image = iio.imread("imageio:astronaut.png") | ||
|
|
||
| # create a figure with an ImageGraphic | ||
| figure = fpl.Figure(size=(700, 560)) | ||
| image_graphic = figure[0, 0].add_image(initial_image) | ||
|
|
||
|
|
||
| class FileDialogWidget(EdgeWindow): | ||
| """Widget with file dialog to load and display images.""" | ||
|
|
||
| def __init__(self, figure, image_graphic): | ||
| super().__init__( | ||
| figure=figure, | ||
| size=200, | ||
| location="right", | ||
| title="File Dialog", | ||
| ) | ||
| self._image_graphic = image_graphic | ||
| self._file_dialog = None | ||
| self._status_msg = "Click 'Open Image' to load a file" | ||
|
|
||
| def update(self): | ||
| """Render the ImGui interface.""" | ||
| imgui.spacing() | ||
| imgui.text("Load an image file:") | ||
| imgui.spacing() | ||
|
|
||
| # open file button | ||
| if imgui.button("Open Image", imgui.ImVec2(-1, 0)): | ||
| self._file_dialog = pfd.open_file( | ||
| "Select Image File", | ||
| "", | ||
| ["Image Files", "*.png *.jpg *.jpeg *.tif *.tiff", "All Files", "*.*"], | ||
| ) | ||
|
|
||
| # check for dialog result (non-blocking) | ||
| if self._file_dialog is not None and self._file_dialog.ready(): | ||
| result = self._file_dialog.result() | ||
| if result: | ||
| self._load_image(result[0]) | ||
| self._file_dialog = None | ||
|
|
||
| # status display | ||
| imgui.spacing() | ||
| imgui.separator() | ||
| imgui.spacing() | ||
| imgui.text_wrapped(self._status_msg) | ||
|
|
||
| def _load_image(self, filepath): | ||
| """Load an image file and update the graphic.""" | ||
| try: | ||
| img_data = iio.imread(filepath) | ||
|
|
||
| # validate image format: must be 2D (grayscale) or 3D with 1-4 channels | ||
| if not (img_data.ndim == 2 or (img_data.ndim == 3 and img_data.shape[2] <= 4)): | ||
| self._status_msg = ( | ||
| f"Unsupported format: {img_data.shape}. " | ||
| f"Expected (H, W) or (H, W, C) with C <= 4" | ||
| ) | ||
| return | ||
|
|
||
| # check if the new image shape matches the current buffer | ||
| if img_data.shape == self._image_graphic.data.value.shape: | ||
| self._image_graphic.data = img_data | ||
| else: | ||
| # shape mismatch: remove old graphic and create a new one | ||
| subplot = self._image_graphic._plot_area | ||
| subplot.remove_graphic(self._image_graphic) | ||
| self._image_graphic = subplot.add_image(img_data) | ||
| subplot.auto_scale() | ||
|
|
||
| self._status_msg = f"Loaded: {filepath}" | ||
| except Exception as e: | ||
| self._status_msg = f"Error: {e}" | ||
|
|
||
|
|
||
| figure.show() | ||
|
|
||
| # add the file dialog widget | ||
| widget = FileDialogWidget(figure, image_graphic) | ||
| figure.add_gui(widget) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(__doc__) | ||
| fpl.loop.run() | ||
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.
EdgeWindowhas afigureinstance so you can use that to get the subplot.Graphic._plot_areaisn't part of the public API