Table Of Contents

Previous topic

View phase difference

Next topic

Generate Spoke Pattern image

Extracting a subimage

Usage

giplt extract.py

Description

This script can be used to extract a subimage either using the graphical selection in the image viewer or by entering the top-left and bottom-right coordinates of the subimage manually.

Script

from iplt.gui.inputdialog import *


def load_image():
    global image,viewer
    load_dialog=InputDialog("Load image")
    load_dialog.AddPath("Filename:","open")
    if load_dialog.exec_():
        image=ost.io.LoadImage(load_dialog.GetData()[0])
        viewer=Viewer(image)
        return True
    return False

def extract_graphical():
    global image,viewer
    image=image.Extract(viewer.GetSelection())
    viewer=Viewer(image)
    return True

def extract_numerical():
    global image,viewer
    ext=image.GetExtent()
    range_dialog=InputDialog("Extract range:")
    range_dialog.AddInt("x1: ",ext.GetStart()[0],ext.GetEnd()[0],ext.GetStart()[0])
    range_dialog.AddInt("y1: ",ext.GetStart()[1],ext.GetEnd()[1],ext.GetStart()[1])
    range_dialog.AddInt("x2: ",ext.GetStart()[0],ext.GetEnd()[0],ext.GetEnd()[0])
    range_dialog.AddInt("y2: ",ext.GetStart()[1],ext.GetEnd()[1],ext.GetEnd()[1])
    if range_dialog.exec_():
        data=range_dialog.GetData()
        image=image.Extract(ost.img.Extent(ost.img.Point(data[0],data[1]),ost.img.Point(data[2],data[3])))
        viewer=Viewer(image)
        return True
    return False

def save_image():
    global image,viewer
    save_dialog=InputDialog("Save image")
    save_dialog.AddPath("Filename: ","save")
    if save_dialog.exec_():
        ost.io.SaveImage(image,save_dialog.GetData()[0])
        return True
    return False

dock = QWidget()
vb=QVBoxLayout()

pb=QPushButton('Load image')
QObject.connect(pb,SIGNAL("clicked()"),load_image)
vb.addWidget(pb)

pb=QPushButton('Take subimage extent form selection')
QObject.connect(pb,SIGNAL("clicked()"),extract_graphical)
vb.addWidget(pb)

pb=QPushButton('Enter subimage extent manually')
QObject.connect(pb,SIGNAL("clicked()"),extract_numerical)
vb.addWidget(pb)

pb=QPushButton('Save image')
QObject.connect(pb,SIGNAL("clicked()"),save_image)
vb.addWidget(pb)

dock.setLayout(vb)
dockwidget=ost.gui.Widget(dock)
app=ost.gui.GostyApp.Instance()
app.perspective.panels.AddWidgetToPool("Subimage extraction",dockwidget)
app.perspective.panels.AddWidget(ost.gui.PanelPosition.LEFT_PANEL, dockwidget)
dock.show()