0

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 "input file" and line 10 gives the location of "output file".

I want to create a user interface to give this location in the user interface itself and run the code from user interface itself.

I have tried making a user interface using "tkinter" module :

from tkinter import *
from tkinter import filedialog
def input():
    file1 = filedialog.askopenfile()
    label = Label(text=file1).pack()
def input2():
    file2 = filedialog.asksaveasfile(mode="w", defaultextension=".tif")
    label = Label(text=file2).pack()    
w = Tk()
w.geometry("500x500")
w.title("FLOOD_MAPPER")
h = Label(text = "S1A FLOOD MAPPER", bg = "yellow", fg = "black", height = "3", width = "500")
h.pack()
i1 = Label(text = "Input*")
i1.place(x=10, y=70)
i1b = Button(w, text = "Select File", command =input)
i1b.place(x=250, y=70)
i2 = Label(text = "Intermediate Product*")
i2.place(x=10, y=140)
i2b = Button(w, text = "Save as", command =input2)
i2b.place(x=250, y=140)
button = Button(w, text="Generate Map", bg = "red", fg = "black", height = "2", width="30")
button.place(x=150, y=400)
w.mainloop()

But I didn't understand how to link these two codes.

The moment I click on button "generate map" in the user interface I want the location/path of Input and output given in the user interface box to move to their respective places in the 1st code and then run the same code aumoatically.

Kindly, help me to achieve my requirement.

4
  • put you code in function ie. def my_code() and assing to button Button( command=my_code) Commented Apr 21, 2019 at 17:51
  • @furas What will it do exactly, if I do so. Commented Apr 21, 2019 at 17:57
  • it will run your code when you press button. Commented Apr 21, 2019 at 18:04
  • @that's fine. What about passing the input and output location ? Commented Apr 21, 2019 at 18:08

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

9 Comments

can you explain me whats happening in def gen_map():
when you use dialogs to select files then tehy keep filenames in viarables input_filename and output_filename. gen_map gets filenames from input_filename and output_filename and runs function you_code with these filenames. And your code gets these filenames and use them to read and write data. command= expects function name without () and arguments so I can't assing command= your_code(input_filename, output_filename) - this is why I created gen_map to assign it as command=gen_map
encountered with following error : Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__ return self.func(*args) File "<ipython-input-4-675a3c59d479>", line 50, in gen_map your_code(input_filename, output_filename) File "<ipython-input-4-675a3c59d479>", line 21, in your_code outdata = driver.Create(output_file, 14823, 9985, 1, gdal.GDT_UInt16) File "C:\Users\Anaconda3\lib\site-packages\osgeo\gdal.py", line 1743, in Create return _gdal.Driver_Create(self, *args, **kwargs) RuntimeError: not a string
I changed asksaveasfile into asksaveasfilename one hour ago - maybe you use old version. asksaveasfilename gives name of file, asksaveasfile was giving object file (result of open(selected_filename)). Your code need first one - asksaveasfilename - which give name of file, not file object.
you can add Label and change text in it. In current code I change text in Label to display selected filename. Tkitner has also progressbar but if you start long-running process then mainloop waits for its end and it can't update progressbar (and it also can't response for clicked buttons or update text in Label or status bar.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.