Skip to content
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

Add screenshot button #41

Merged
merged 12 commits into from
Apr 14, 2022
34 changes: 33 additions & 1 deletion ocrd_browser/view/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def set_page(self, page: Page) -> None:
versions.append(ImageVersion.from_page(self.document, page))
alts: List[AlternativeImageType] = page.page.get_AlternativeImage()
for alt in alts:
versions.append(ImageVersion.from_alternative_image(self.document, alt))
if self.document.path(alt.filename).exists():
versions.append(ImageVersion.from_alternative_image(self.document, alt))

with self.version_box.handler_block(self._change_handler):
self.versions.clear()
Expand Down Expand Up @@ -255,6 +256,10 @@ def build(self) -> None:
self.add_configurator('scale', ImageZoomSelector(2.0, 0.05, -4.0, 2.0))
self.add_configurator('image_version', ImageVersionSelector())
self.add_configurator('features', PageFeaturesSelector())
icon = Gtk.Image.new_from_icon_name('camera-photo', Gtk.IconSize.SMALL_TOOLBAR)
button = Gtk.Button(image=icon, visible=True, always_show_image=True, tooltip_text='Saves a screenshot of the current view')
button.connect('clicked', self.open_screenshotdialog)
self.action_bar.pack_start(button)

actions = ActionRegistry()
actions.create(name='zoom_by', param_type=GLib.VariantType('i'), callback=self._on_zoom_by)
Expand Down Expand Up @@ -530,3 +535,30 @@ def update_transformation(self) -> None:
self.page_image.height
)
self.highlight.queue_draw()

def open_screenshotdialog(self, button: Gtk.Button) -> None:
if self.page_image is None:
return

dialog = Gtk.FileChooserDialog(title="Save image under...",
parent=self.window,
action=Gtk.FileChooserAction.SAVE)
dialog.add_buttons(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE,
Gtk.ResponseType.OK)
filter_png = Gtk.FileFilter()
filter_png.set_name("PNG image files")
filter_png.add_mime_type("image/png")
dialog.add_filter(filter_png)
dialog.set_current_name("untitled.png")

response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
else:
filename = ''

dialog.destroy()
if filename:
self.page_image.save(filename)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[flake8]
ignore=E501
exclude=tests/assets/__init__.py

[mypy]
warn_return_any = True
Expand Down
2 changes: 1 addition & 1 deletion tests/util/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _image_modes():
class ImageUtilTestCase(TestCase):

def test_pil_to_pixbuf_is_faster_via_opencv(self):
# self.skipTest('Slow test')
self.skipTest('Slow test')
files = [
ASSETS_PATH / 'kant_aufklaerung_1784-binarized/data/OCR-D-IMG/OCR-D-IMG_0017.tif',
ASSETS_PATH / 'kant_aufklaerung_1784-binarized/data/OCR-D-IMG-1BIT/OCR-D-IMG-1BIT_0017.png',
Expand Down