Search code examples
svgimagemagickpnggimp

How to change a single colour in an image when converting to a png?


Goal: convert an icon (single-colour, black, with transparent background) from either an svg or xcf, to a png and change the colour of the icon (e.g. black becomes white or a specified hex colour).

ImageMagick version: 7.0.11-6

Progress so far: I have a Windows batch file that

  • for each xcf (Gimp) file in the folder will
    • for each file col-*.* in the folder
      • resize, resample and then remap against the col-*.* file and save the png

I have had some success, but have found that

  • the col- files can't be all one colour (the colour I want) otherwise the whole new png is just that block colour too.
  • if the col- files are pngs with the colour I want and a transparent background it works with dark colours, but lighter colours end up fuzzy around the edges.
  • -remap does not work with svg source files and using -fill and a hex value gets a fuzziness on all png images created.

Is there a better way to re-colour these pngs? Or a way to stop the fuzziness? I have just started working with ImageMagick. Many thanks!

Current batch file

@echo off

set resolution=72
set width=48

setlocal enableDelayedExpansion

for %%i in (*.xcf) do (
    echo Converting [ %%i --^> %%~ni.png ] ^(%width%@%resolution%^)
    
    for %%x in (os-col-*.*) do (        
        magick %%i -resize %width% -resample %resolution% -remap %%x %%~ni-%width%-%%x.png
        echo Done %%i - %%x
    )
    
    echo Done %%i
)

black png, no fuzziness

black png, no fuzziness

fuzziness in a png with -remap applied

fuzziness in a png with -remap applied


Solution

  • Using ImageMagick, if your input is a single color and you want to change it to another while retaining the transparency, this command should do exactly that...

    convert input.png -fill #75965b -colorize 100 result.png
    

    That creates this output image....

    enter image description here

    That is in IMv6 syntax. For IMv7 use "magick" instead of "convert".