[gdal-dev] How to convert png color to png/tiff black and white

LE LAMER Romain rlelamer at hotmail.fr
Sat Jan 4 04:17:38 PST 2020


Hi,

Sorry to answer that now I have not had the notification ... (it was in my spam)

I used software to binarize my png which allowed me to carry out my project until the end.

But I like the idea of being able to process this via a script.
I have a script_list.sh which contains a script_file.sh for each tile which allows me to automate the tasks for the 512 tiles.
I will test your solution and incorporate it as soon as I have time (I want to finalize the last things I have left before) and I will keep you posted.

I'm on mac but I installed gawk and gsed (awk and sed are different on macOS)

Thanks again for the code on github ;)

Le 3 janv. 2020 à 01:42, lars.schylberg at blixtmail.se<mailto:lars.schylberg at blixtmail.se> a écrit :

Hi,

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.

https://gist.github.com/LarsSchy/c1be96fa03f7dc11df4247be033e2705

I hope this helps You.

Have fun / Lars Schylberg

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20200104/7f01d394/attachment.html>


More information about the gdal-dev mailing list