ValueError: Received a null pointer

Viewed 719

I'm writing a code to convert GeoTiff files to png. Pillow didn't work for me. Hence I am using gdal. Am I missing something in the code? The code is as below:

from osgeo import gdal
import os
options_list = ['-ot Byte','-of PNG'] 
options_string = " ".join(options_list)
in_tif = r"E:\some\path\tif_folder"
out_png = r"E:\some\other\path\png_folder"
list_tif = os.listdir(in_tif) 
for x in list_tif:
    in_tif = in_tif + "\\" + x
    out_png = out_png + "\\" + x
    gdal.Translate(out_png,in_tif,options=options_string)
1 Answers

It actually gave me a file with .tif extension which I previously failed to notice. Thus, I used replace function to change tif to png. There was also very basic flaw in the for loop because of which the file was kept getting added. new code which works:

    from osgeo import gdal
    import os
    options_list = ['-ot Byte','-of PNG'] 
    options_string = " ".join(options_list)
    in_tif = r"E:\some\path\tif_folder"
    out_png = r"E:\some\other\path\png_folder"
    list_tif = os.listdir(in_tif) 
    for x in list_tif:
        indi_tif = in_tif+"\\" + x
        print(in_tif)
        indi_png = out_png + "\\" + x
    try:

gdal.Translate(indi_png.replace(".tif",".png"),indi_tif,options=options_string)

    except: 
        print("Failed files,",indi_tif) 
Related