Search code examples
imagemagickimagemagick-convert

Imagemagick: channel gets lighter after separate/combine operation


I'm trying to combine PBR normal/roughness/metallic map images (.png) into a single RGBA image (also .png), with the normal map in the red and green channels, the roughness map in the blue channel, and the metallic map in the alpha channel.

The following bash command line works, but for some reason the green channel is slightly lighter in the composite image than it is in the original normal image (e.g. a value of 0x7e in the original is raised to 0x87 in the composite). All three other channels keep their original values. If I switch the order of the red and green separation, it's the red channel in the final image that gets lighter. What could be causing that, and how do I avoid it?

convert ImageName_Normal.png -colorspace sRGB -write mpr:NORMAL_R -channel R -separate +channel \
-write mpr:NORMAL_G -channel G -separate +channel +delete \
ImageName_Roughness.png -colorspace Gray -flatten -write mpr:ROUGHNESS +delete \
ImageName_Metallic.png -colorspace Gray -flatten -write mpr:METALLIC +delete \
mpr:NORMAL_G mpr:NORMAL_R mpr:ROUGHNESS -combine -write mpr:RGBCOMP +delete \
mpr:RGBCOMP mpr:METALLIC -alpha off -compose Copy_Opacity -composite -depth 8 \
png32:ImageName_NormRoughMetal.png

ImageMagick version is 6.9.11-60

I tried different variations on it - using \( +clone , another copy of the normal map (with and without +delete after the first one), and so on. None of them worked any better (and some actually gave me two copies of the normal map's red channel even though green was specified on the command line for the second one).


Solution

  • I do not think your command is what you want in Imagemagick. I think you are not keeping the first two parts properly separate.

    Try this

    convert TestImage_Normal.png -colorspace sRGB -write mpr:NORMAL +delete \
    \( mpr:normal -channel R -separate +channel -write mpr:NORMAL_R +delete \) \
    \( mpr:normal -channel G -separate +channel -write mpr:NORMAL_G +delete \) \
    \( TestImage_Roughness.png -colorspace Gray -flatten -write mpr:ROUGHNESS +delete \) \
    \( TestImage_Metallic.png -colorspace Gray -flatten -write mpr:METALLIC +delete \) \
    \( mpr:NORMAL_G mpr:NORMAL_R mpr:ROUGHNESS -combine -write mpr:RGBCOMP +delete \) \
    mpr:RGBCOMP mpr:METALLIC -alpha off -compose Copy_Opacity -composite -depth 8 \
    png32:TestImage_NormRoughMetal_fred.png