0

I am working with a grey scale image from a DEM. No matter how much Googling or reading I do I can NOT find a solution for Getting elevation values from a raster images pixels. It should be as simple as referencing a legend and seeing that black equals X value and White equals Y value and interpolating the grey scale values in between. However this task seems impossible without libraries like GDAL or ArcGIS. I simple need to take an image in Python and grab a specific pixel then find the value within the pixel that from my understanding matches the grey scale value and a corresponding height.

I am looking to write this implementation in Python as I am stronger with the language and understand how to accomplish my goals using standard and additional libraries. I am writing a mobile application that mainly requires one thing... offline elevation data globally. I have my dataset that holds this information in raster images. I know the coordinates of every pixel. The only unknown is getting the elevation from each pixel. Each pixel is in grey scale, black being ZERO feet above sea level and white being the highest elevation. I am assuming that each intensity increase from one darker shade of grey to a lighter/more intense shade of grey is a 1 unit increase in elevation above sea level. I will correct for gravity later as I have completed this part. I simple need to know the implementation that takes the PNG file, then gives me the value of color for each pixel of which I can later change to coordinates and that's it. So I am avoiding libraries and will likely need to rewrite this code in Swift later when I decide to port this functionality over to the app. I am holding off on jumping directly into swift because I can complete a working example faster and which better understanding. I would not attempt to learn a new Math topic in a language I do not speak as I would be fighting the language and not grasping the topic.

How would I go about getting a single greyscale color value of a given pixel in an image in the Python language if I chose to avoid using common libraries and stuck to using only the Standard Python Library?

3
  • GDAL with python > gis.stackexchange.com/questions/341014/…
    – Mapperz
    Commented Oct 29, 2020 at 1:33
  • 3
    It's unlikely that pixel value of a PNG equates to the elevation of that pixel. A greyscale PNG would have values from 0-255, these aren't the elevations. I would not assume that each shade of grey is a 1 unit increase in elevation. You should be looking at raster types which are suitable for storing Digital Elevation Models (DEM) and not standard image types such as png or jpg.
    – Fezter
    Commented Oct 29, 2020 at 2:52
  • 1
    You're really trying to do this the hard way by not using external libs, without these you would need to open the raster as a binary file, interrogate the header, apply the geotransform for screen X,Y to world X, Y (or lat, lon) then to pixel row, col IMO this is beyond python and needs unsafe C.. As @Fezter has pointed out it's unlikely that your elevation value is in the PNG libpng.org/pub/png/book/…. has a list and none of the options are floating point or even 16 bit integer. Commented Oct 29, 2020 at 4:53

1 Answer 1

1

If you have a file with values between 0 and 255, you will never get an elevation. You need a proper DEM file for that. Usually, that is a .tiff or .hgt file but other formats are used. You could get a height if you had a ratio between 0 -255 and the min z and max z but that would change on every file. You should use a real DEM. You could go the GDAL source code and make your own driver but that would be pain. Specially if you are trying not to use numpy.

1
  • I doubt the GDAL source would help, it's C++ with low level interfaces relying on 3rd party libs. Approximating from a greyscale is impossible, there just isn't enough information, you can't say absolutely that the stretch is linear and not logarithmic, standard deviation or some other multivariate where the rate of grey change increases or decreases over elevation ranges. Variations of this question have been asked many times on this site and the answer is always the same as yours. eg: gis.stackexchange.com/questions/185530/… Commented Oct 29, 2020 at 6:37

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