0

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

  1. I use tkinter and the .filedialog.askopenfilename function to get user selected file directory name.
  2. The selected file directory name is assigned to variable "assigned_file_directory".
  3. This variable is used as the file path for gdal.open so it can be read as an array via the GDAL .ReadAsArray function.
  4. 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.

1
  • I would compare the "current_opened_file" that works and the "assigned_file_directory" that does not work. They can't be equal.
    – user30184
    Commented Dec 4, 2022 at 20:51

1 Answer 1

0

I figured out the answer to anyone in the future with the same issue.

Turns out I needed to assign the current_opened_file as a global variable like so:

if (select_file_name):
    global current_opened_file
    current_opened_file = select_file_name
    current_selected_file.configure(text = "Current Selected File: " + 
    current_opened_file)

This changes the variable so it can be assigned new values outside of the function.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.