[gdal-dev] How to convert png color to png/tiff black and white
lars.schylberg at blixtmail.se
lars.schylberg at blixtmail.se
Thu Jan 2 16:42:52 PST 2020
There is one way to solve this with gdal_calc. I have had some time off during the holidays,
but kept thinking about your question and thought it was a fun problem.
Actually I have solved some other problems with tif palette images and gdal_calc lately. So I put together one solution that
I think will work in your case. I am assuming that You are using linux and have some knowledge in bash scripting.
First we have to create a reclass rule for gdal_calc, We do that with with running gdalinfo.
Strip the info to only keep the color table with sed.
Then dynamically create the rule with awk, based on your rgb values (turn palette values into either 0 or 1).
Finally strip the last "+" from the rule (the last sed command)
Then run gdal_calc with your relclass rule
gdal_calc produces a grey scale image, but:
We then turn the image into a virtual raster file (VRT)
We create a new color table and insert it into the vrt file with sed.
Change the vrt file to be a palette image again with sed.
Finally we can turn the vrt image into a tif image again.
If this works you could loop over all your images and process all in the same way.
Just do a bash function with the steps and loop over your input files.
The whole idea of the process is outlined in a gist I just uploaded.
I just tested the first part with the rule myself, but the other stuff I used before
so it should work.
I hope this helps You.
Have fun / Lars Schylberg
29 december 2019 kl. 16:47, "LE LAMER Romain" <rlelamer at hotmail.fr (mailto:rlelamer at hotmail.fr?to=%22LE%20LAMER%20Romain%22%20<rlelamer at hotmail.fr>)> skrev:
I need to convert 524 288 color png tiles to black and white png.
=> The lands always have the 2 colors RGB (RGBA?) 59,89,95,255 and / or 60,89,96,255.
=> The sea always has the colors RGB 109,178,200,255 / 138,193,212,255 and 161,205,220,255.
Some tiles contain only land, others only sea and still others contain land and at least 1 color of the sea ...
I use gdal_translate to georeference and assign a projection to each tile (via script) with the following command (and it works perfectly):
gdal_translate -a_srs EPSG: 4326 -a_ullr <ulx> <uly> <lrx> <lry> input.png output.tiff
I have read and reread, in every way, the gdal_translate help with the args -b and -mask and I don't understand how to use it ...
I need all the sea colors to always be Black and all the lands colors to be White.
(I polygonize each tile afterwards)
How can I do this via gdal_translate or other gdal program?
Thanks for your help
What gdalinfo gives me for a random tile (original png)
gdalinfo map_11_0_309.png Driver: PNG/Portable Network Graphics Files: map_11_0_309.png Size is 256, 256 Coordinate System is `' Corner Coordinates: Upper Left ( 0.0, 0.0) Lower Left ( 0.0, 256.0) Upper Right ( 256.0, 0.0) Lower Right ( 256.0, 256.0) Center ( 128.0, 128.0) Band 1 Block=256x1 Type=Byte, ColorInterp=Palette Image Structure Metadata: NBITS=2 Color Table (RGB with 4 entries) 0: 60,89,96,255 1: 109,178,200,255 2: 138,193,212,255 3: 161,205,220,255
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the gdal-dev