from osgeo import gdal from PIL import Image import csv import traceback # creating a image object im = Image.open('nadia_banana.png.png') im = im.convert('RGB') #px = im.load() # Open tif file ds = gdal.Open('Nadia.tif') ds_meta = ds.GetMetadata() # width w = ds.RasterXSize # height h = ds.RasterYSize im = im.resize((w,h)) pixel_data = ds.ReadAsArray() #print(pixel_data) # GDAL affine transform parameters, According to gdal documentation xoff/yoff are image left corner, a/e are pixel wight/height and b/d is rotation and is zero if image is north up. xoff, a, b, yoff, d, e = ds.GetGeoTransform() def pixel2coord(x, y): """Returns global coordinates from pixel x, y coords""" xp = a * x + b * y + xoff yp = d * x + e * y + yoff return(xp, yp) # get columns and rows of your image from gdalinfo print([xoff, a, b, yoff, d, e]) rows = h colms = w mark_num = 0 all_markers = [] if __name__ == "__main__": for row in range(0,rows): for col in range(0,colms): #print(pixel2coord(col,row)) #print([row, col, im.getpixel((col,row))]) try: pix_val = im.getpixel((col,row)) if pix_val[1] > 5*pix_val[0] and pix_val[0] > 2 and pix_val[2] > 2: #if im.getpixel((col,row)) != (0,0,0) and im.getpixel((col,row)) != (255,255,255): print(im.getpixel((col,row))) #print([row, col, im.getpixel(pixel2coord(col,row))]) all_markers.append(pixel2coord(col,row)) mark_num = mark_num + 1 except: print(col, row) print(mark_num) with open('banana.csv', 'w') as f: # using csv.writer method from CSV package write = csv.writer(f) write.writerows(all_markers)