spacr.qt.widgets.picture_export¶
Save a rendered picture as a PNG or a one-page PDF.
Exports use the supplied QImage or QPixmap rather than capturing
the surrounding widget. PNG preserves the image’s pixel dimensions and
records the selected resolution as dots-per-metre metadata. PDF embeds
the raster image on a page; it does not convert pixels into vector artwork.
PDF page dimensions are computed from the image’s pixel dimensions and export DPI, with a minimum of one millimetre for each dimension. The image fills the page with zero margins. This sets the printed size without using microscope pixel calibration.
Default resolution and filename suffix come from
spacr.qt.preferences.get_figure_png_dpi() and
spacr.qt.preferences.get_figure_format(). The right-click menu’s
explicit PNG or PDF choice determines the final export format.
Functions¶
|
Whatever was handed over, as a |
|
Ask for an output path; return |
|
The right-click menu: save as PNG, save as PDF. |
|
Pop the menu at |
|
Give |
|
The resolution a saved picture is written at. |
|
|
|
Ask for a path and write |
|
Write |
|
A file name for |
Module Contents¶
- spacr.qt.widgets.picture_export.as_image(picture) PySide6.QtGui.QImage | None[source]¶
Whatever was handed over, as a
QImage, orNone.A caller may hold a
QPixmap(what a view shows), aQImage(what a renderer produced) or nothing at all yet.- Parameters:
picture – a
QPixmap, aQImageorNone; a null image or any other type also givesNone.
- spacr.qt.widgets.picture_export.ask_where_to_save(parent, stem: str) str[source]¶
Ask for an output path; return
""when the dialog is cancelled.If the chosen name has no suffix, append
.pdfor.pngaccording to the selected file filter. An existing suffix is retained here;save_as()subsequently enforces the format chosen in the menu.- Parameters:
parent – the widget the save dialog is parented to.
stem – the file name offered, without a suffix; cleaned by
suggested_name().
The right-click menu: save as PNG, save as PDF.
Each action carries its suffix as its
data, so the caller reads the choice off the action rather than comparing it against two references it had to keep.- Parameters:
parent – the widget that owns the menu.
enabled –
Falsewhen there is no picture yet. The actions are SHOWN AND GREYED rather than left out, and a line underneath says why: a menu that is empty on one field and full on the next reads as a bug, not as a state.
- spacr.qt.widgets.picture_export.choose_format(view, point, enabled: bool) str[source]¶
Pop the menu at
pointand return the chosen suffix, or"".THE MODAL CALL IS ALONE IN HERE so everything around it can be driven by a test.
QMenu.execspins an event loop of its own, and a test that reached it would hang rather than fail; substituting this one function is how the rest of the gesture is checked at all.- Parameters:
view – the widget the menu belongs to;
pointis mapped to global coordinates through it.point – where the right-click happened, in
viewcoordinates.enabled –
Falsegreys out the save actions, as inbuild_menu().
- spacr.qt.widgets.picture_export.install_picture_save(view, picture: Callable[[], object], stem: str = 'picture', unless: Callable[[], bool] | None = None) bool[source]¶
Give
viewa right-click menu that saves what it is showing.- Parameters:
view – the widget the user right-clicks.
picture – called before the context menu opens; returns the
QPixmaporQImageto write. The returned image is used after the format and path dialogs; the callback is not called again immediately before writing.stem – the file name offered, without a suffix. A callable is called after the format is selected and before the path dialog opens.
unless – asked first;
Truemeans the right button belongs to something else at this moment and no menu is shown. The live preview’s ruler is cleared with a right-click while it is active, and a menu appearing over that would take a tool away to add a file dialog nobody asked for.
- Returns:
whether the menu was installed.
- spacr.qt.widgets.picture_export.picture_dpi() int[source]¶
The resolution a saved picture is written at.
- Returns:
the Figures preference, or
FALLBACK_DPI.
- spacr.qt.widgets.picture_export.preferred_suffix() str[source]¶
".pdf"or".png", following the Figures format preference.
- spacr.qt.widgets.picture_export.save_as(view, picture, stem: str, want: str) str[source]¶
Ask for a path and write
picturethere.""if nothing was.- Parameters:
view – the widget the save dialog and any warning are parented to.
picture – the
QPixmaporQImageto write.stem – the file name offered, without a suffix.
want – the suffix the user picked in the menu. It WINS over what the file dialog came back with, so “save as PDF” followed by a name ending in
.pngstill writes a PDF rather than quietly changing format because of a typed extension.
- Returns:
the path written, or
"".
- spacr.qt.widgets.picture_export.save_picture(picture, path, dpi: int | None = None) bool[source]¶
Write
picturetopath, as a PNG or a PDF by its suffix.- Parameters:
picture – a
QPixmaporQImage.path – where to write.
.pdfwrites a page; anything else writes a PNG.dpi – resolution;
picture_dpi()when omitted.
- Returns:
whether a file was written.
Nested helpers¶
Offer the save menu at
point; the path written, or"".- Parameters:
point – where the right-click landed, in
viewcoordinates.
spacr/qt/widgets/picture_export.py:335