Skip to content Skip to sidebar Skip to footer

How To Give The Location Of "input" And "output" For A Python Code Using User Interface And Run The Code From Ui Itself?

I have a python code : import gdal import numpy from skimage.filters import threshold_otsu ds = gdal.Open('A:\\algo\\f2.tif') In this code, line 4 gives the location/path of 'inpu

Solution 1:

It can look like this. I removed keep only important elements in tkinter.

I put code in your_code and it can get filenames as paramaters. So this code looks similar as before.

I create function gen_map which get run your_code with filenames which are assigned to global variables input_filename, `output_filename.

I assing gen_map to button Button( command=gen_map) so it will run it when you press button.

Other buttons open dialog to get file names and assign to global variables input_filename, output_filename.

from tkinter import *
from tkinter import filedialog

import gdal
import numpy
from skimage.filters import threshold_otsu

def your_code(input_file, output_file):

    #ds = gdal.Open('A:\\algo\\f2.tif')

    ds = gdal.Open(input_file)

    band = ds.GetRasterBand(1)
    arr = band.ReadAsArray()
    thresh = threshold_otsu(arr,16)
    binary = arr > thresh
    driver = gdal.GetDriverByName("GTiff")

    #outdata = driver.Create("A:\\algo\\test11.tif", 14823, 9985, 1, gdal.GDT_UInt16)
    outdata = driver.Create(output_file, 14823, 9985, 1, gdal.GDT_UInt16)

    outdata.SetGeoTransform(ds.GetGeoTransform())
    outdata.SetProjection(ds.GetProjection())
    outdata.GetRasterBand(1).WriteArray(binary)
    outdata.GetRasterBand(1).SetNoDataValue(10000)
    outdata.FlushCache() ##saves to disk!!

    #outdata = None
    #band = None
    #ds = None

def get_input_filename():
    global input_filename

    # `askopenfilename` instead of `askopenfile` to get filename instead of object file
    input_filename = filedialog.askopenfilename()
    input_label['text'] = input_filename

def get_output_filename():
    global output_filename

    # `asksaveasfilename` instead of `asksaveasfile` to get filename instead of object file
    output_filename = filedialog.asksaveasfilename(defaultextension=".tif")
    output_label['text'] = output_filename

def gen_map():
    #global input_filename
    #global output_filename
    print('input:', input_filename)
    print('output:', output_filename)

    your_code(input_filename, output_filename)

#---------------------------------------------
# global variables with default values at start

input_filename = 'A:\\algo\\f2.tif'
output_filename = "A:\\algo\\test11.tif"

root = Tk()

#input_label = Label(root, text=input_filename)
input_label = Label(root, text="Input*")
input_label.pack()

input_button = Button(root, text="Select File", command=get_input_filename)
input_button.pack()

#output_label = Label(root, text=output_filename)
output_label = Label(root, text="Intermediate Product*")
output_label.pack()

output_button = Button(root, text="Save as", command=get_output_filename)
output_button.pack()

gen_map_button = Button(root, text="Generate Map", command=gen_map)
gen_map_button.pack()

root.mainloop()

Post a Comment for "How To Give The Location Of "input" And "output" For A Python Code Using User Interface And Run The Code From Ui Itself?"