<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">Hi,<br><br>There is one way to solve this with gdal_calc. I have had some time off during the holidays,<br>but kept thinking about your question and thought it was a fun problem.<br>Actually I have solved some other problems with tif palette images and gdal_calc lately. So I put together one solution that<br>I think will work in your case. I am assuming that You are using linux and have some knowledge in bash scripting.<br><br>First we have to create a reclass rule for gdal_calc, We do that with with running gdalinfo. <br>Strip the info to only keep the color table with sed.<br>Then dynamically create the rule with awk, based on your rgb values (turn palette values into either 0 or 1).<br>Finally strip the last "+" from the rule (the last sed command)<br><br>Then run gdal_calc with your relclass rule<br><br>gdal_calc produces a grey scale image, but:<br><br>We then turn the image into a virtual raster file (VRT)<br>We create a new color table and insert it into the vrt file with sed.<br>Change the vrt file to be a palette image again with sed.<br>Finally we can turn the vrt image into a tif image again.<br><br>If this works you could loop over all your images and process all in the same way.<br>Just do a bash function with the steps and loop over your input files.<br><br>The whole idea of the process is outlined in a gist I just uploaded.<br>I just tested the first part with the rule myself, but the other stuff I used before<br>so it should work.<br><br><a href="https://gist.github.com/LarsSchy/c1be96fa03f7dc11df4247be033e2705">https://gist.github.com/LarsSchy/c1be96fa03f7dc11df4247be033e2705</a><br><br>I hope this helps You. <br><br>Have fun / Lars Schylberg<br><br>29 december 2019 kl. 16:47, "LE LAMER Romain" <<a target="_blank" tabindex="-1" href="mailto:rlelamer@hotmail.fr?to=%22LE%20LAMER%20Romain%22%20<rlelamer@hotmail.fr>">rlelamer@hotmail.fr</a>> skrev:<br> <blockquote><div><div style="word-wrap: break-word;-webkit-nbsp-mode: space;line-break: after-white-space"><div style="word-wrap: break-word;-webkit-nbsp-mode: space;line-break: after-white-space"> <div>Hi,</div> <div></div> <div>I need to convert 524 288 color png tiles to black and white png.</div> <div></div> <div>=> The lands always have the 2 colors RGB (RGBA?) 59,89,95,255 and / or 60,89,96,255.</div> <div>=> The sea always has the colors RGB 109,178,200,255 / 138,193,212,255 and 161,205,220,255.</div> <div></div> <div>Some tiles contain only land, others only sea and still others contain land and at least 1 color of the sea ...</div> <div></div> <div>I use gdal_translate to georeference and assign a projection to each tile (via script) with the following command (and it works perfectly):</div> <div><i>gdal_translate -a_srs EPSG: 4326 -a_ullr <ulx> <uly> <lrx> <lry> input.png output.tiff</i></div> <div></div> <div>I have read and reread, in every way, the gdal_translate help with the args <i>-b</i> and <i>-mask</i> and I don't understand how to use it ...</div> <div></div> <div>I need all the sea colors to always be Black and all the lands colors to be White.</div> <div>(I polygonize each tile afterwards)</div> <div></div> <div>How can I do this via gdal_translate or other gdal program?</div> <div></div> <div>Thanks for your help</div> <div></div> <div></div> <div>PS:</div> <div>What gdalinfo gives me for a random tile (original png)</div> <div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">gdalinfo map_11_0_309.png </span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Driver: PNG/Portable Network Graphics</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Files: map_11_0_309.png</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Size is 256, 256</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Coordinate System is `'</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Corner Coordinates:</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Upper Left ( 0.0, 0.0)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Lower Left ( 0.0, 256.0)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Upper Right ( 256.0, 0.0)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Lower Right ( 256.0, 256.0)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Center ( 128.0, 128.0)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Band 1 Block=256x1 Type=Byte, ColorInterp=Palette</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Image Structure Metadata:</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">NBITS=2</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">Color Table (RGB with 4 entries)</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">0: 60,89,96,255</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">1: 109,178,200,255</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">2: 138,193,212,255</span></div> <div style="margin: 0px;font-stretch: normal;font-size: 10px;line-height: normal;font-family: Monaco;color: rgb(16, 16, 16);background-color: rgba(255, 255, 255, 0.85098)"><span style="font-variant-ligatures: no-common-ligatures">3: 161,205,220,255</span></div> </div> <div></div> </div></div></div></blockquote> <br><br><signature></signature> </div></body></html>