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.

Sample image

Script

from ex.gui.inputdialog import *
from gui import Procedure


def load_image():
	global image,viewer
	load_dialog=InputDialog("Load image")
	load_dialog.AddPath("Filename:","open")
	if load_dialog.ShowModal():
		image=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.ShowModal():
		data=range_dialog.GetData()
		image=image.Extract(Extent(Point(data[0],data[1]),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.ShowModal():
		SaveImage(image,save_dialog.GetData()[0])
		return True
	return False



procedure=Procedure('Subimage extraction')
step_1=procedure.AddStep(load_image,'Load image',[])
step_2=procedure.AddStep(extract_graphical,'Take subimage extent form selection',[step_1])
step_3=procedure.AddStep(extract_numerical,'Enter subimage extent manually',[step_1])
step_4=procedure.AddStep(save_image,'Save image',[step_1])
Shell.AddProcedure(procedure)

Back to IPLT cookbook

Attachments