First, we add argument to the button we defined in the previous section.
gui.button(self.controlArea, self, label="Load image", callback=self.browse_file)
Then we define our method for browsing files, because we will use QFileDialog
, we
import
from AnyQt.QtWidgets import QFileDialog
def browse_file(self):
filename, _ = QFileDialog.getOpenFileName(
self, 'Open File', '', 'Image Files (*.gif *.jpg *.jpeg *.png *.svg);;All Files (*)'
)
if filename is None:
return
self.filename = filename
self.load_image()
Here we use NumPy, os and Output. So we add this imports:
import os
import numpy as np
from Orange.widgets.widget import Output
def load_image(self):
if self.filename is None:
self.label.setText("No file selected")
img = None
else:
path = os.path.split(self.filename)
name = path[1]
self.label.setText(name)
img = np.array(Image.open(self.filename))
self.Outputs.image.send(img)
In the previous method we used Outputs to send our image, but we yet have to implement Outputs. Directly under metadata we define:
class Outputs:
image = Output("image", np.ndarray, default=True)
We need to use Setting, which will save file, so when we close Orange, the workflow will
still remember the file, when we open Orange again. Under and outside class Outputs
, we
use:
filename = Setting(None)
Till now, our window had resizing enabled. But it's better to disable this, we need to set
resizing_enabled = False
After we run orange-canvas
, we see our final result when we place our widget on the canvas
and click on it.
Warning
⚠️ But if you check console from where we opened Orange, we get UserWarning.- We need to implement summarize.
.. seealso:: - 🔍 :doc:`summarize`