I am trying to write a python app to ask for file directory input via tkinter. I am not sure what is going wrong but it returns an error: data_array = file.ReadAsArray() AttributeError: 'NoneType' object has no attribute 'ReadAsArray'
The process is as follows:
Both the file_browser and convert_file functions get called with a button in tkinter
- I use tkinter and the .filedialog.askopenfilename function to get user selected file directory name.
- The selected file directory name is assigned to variable "assigned_file_directory".
- This variable is used as the file path for gdal.open so it can be read as an array via the GDAL .ReadAsArray function.
- The file directory string isnt recognized by GDAL and gives NoneType error.
from osgeo import gdal
from tkinter import filedialog
current_opened_file = "D:/Random Stuff/image.tif"
assigned_file_directory = ""
def file_browser():
select_file_name = filedialog.askopenfilename(initialdir = "/", title = "Select a File",)
if (select_file_name):
current_opened_file = select_file_name
current_selected_file.configure(text = "Current Selected File: " + current_opened_file)
split_function = select_file_name.split("/")
split_file_name = (split_function[0] + "//" + split_function[1] + "//" + split_function[2])
assigned_file_directory = '"' + select_file_name + '"'
print(assigned_file_directory)
def convert_file():
#assigning the file path for the tif image
#file_path = current_opened_file
file_path = assigned_file_directory
#opening the tif file and assigning it
file = gdal.Open(file_path)
#the assigned file is converted into an array
data_array = file.ReadAsArray()
#creating the figure dimensions
mlab.figure(size = (1025, 1025))
mlab.surf(data_array, warp_scale = 0.01, colormap = "cool")
mlab.show()
I originally just had the current_opened_file variable change to whatever file was loaded via tkinter as well as I previously left the file directory name as it was. However, I read that GDAL doesn't like single "/" characters and decided to deconstruct the file directory name and manually redo it. I also tested whether quotation marks would make a difference and it did not.
Additionally, the original file loaded will run perfectly fine, however, if I choose to browse for the same file and load it, GDAL gives me the same NoneType error. This makes me believe that my code is correct but perhaps there is an issue with GDAL reading the string directory name.