Search code examples
imagemagickcompositegrayscale

Imagemagick how to compose colored png on black and white image without losing the colors?


I am trying to compose my colored png with red dots on a grayscale image using Imagemagick, but the result i got is a black and white image with white dots.

I am using this command line:

composite -tile -compose Hardlight red-dots.png image.jpg result.jpg

This is the result: enter image description here

In Photoshop i can get what i want by changing the image.jpg mode to RVB Colors, and i obtain this:

enter image description here

Below are the samples i am using:

enter image description here enter image description here


Solution

  • Here is one way to do that in ImageMagick.

    Input:

    enter image description here

    Tile:

    enter image description here

    Line 1: convert command
    Line 2: read the tile, save to MPR, then delete the image (keeping the mpr)
    Line 3: read the face image and save to MPR, but keep the image
    Line 4: use the image and draw the tile over it to fill the dots out to the size of the image
    Line 5: bring the mpr face image back again
    Line 6: swap it with the filled out dots image
    Line 7: apply headlight composition
    Line 8: save the result
    
    
    convert \
    red_dots.png -write mpr:dots +delete \
    man_face.jpg -write mpr:face \
    -fill mpr:dots -draw 'color 0,0 reset' \
    mpr:face \
    +swap \
    -compose hardlight -composite \
    result.png
    

    Result:

    enter image description here

    An alternate way is as follows:

    Line 1: convert command
    Line 2: read the face image
    Line 3: set the distort viewport to the size of the image
    Line 4: read the dots image
    Line 5: do a distort with no change of the image orientation or size to tile it out to the size of the face image
    Line 6: do hard light composite
    Line 7: save the result
    
    
    convert \
    man_face.jpg \
    -set option:distort:viewport '%g' \
    \( red_dots.png \
    -virtual-pixel tile -filter point -distort SRT 0 \) \
    -compose hardlight -composite \
    result2.png
    

    enter image description here