1

I am trying to read the RGB values at certain locations on my map which is a .tif-file I brought into QGIS.

Goal: match species presence points on my map to certain RGB values on the .tif-file.

from PIL import Image
img = PIL.Image.open(iface.layerTreeView().selectedLayers())
col = img.convert("RGB")
val = col.getpixel((0,0)) #tried to read value at center
print (val)

And getting the error:

AttributeError: 'list' object has no attribute 'read'
Taras
  • 32,823
  • 4
  • 66
  • 137
avinator
  • 127
  • 1
  • 6

1 Answers1

1

Right now, I think you are trying to pass a raster as an input to the .open function, which is not an acceptable parameter.

According to the documentation for the Image module, parameters are:

  1. fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
  2. mode – The mode. If given, this argument must be "r".

Try putting the .tif filename directly into the line, like this:

img = PIL.Image.open("image.tif") 

Or providing the full path name as a parameter, like this:

img = PIL.Image.open(r"C:\temp\image.tif") 

Perhaps you might also consider using GDAL to accomplish your tasks. There is an answer here that might help get you started with that.

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389