How to use Imagick to merge and mask images?

前端 未结 4 943
半阙折子戏
半阙折子戏 2020-12-02 08:05

I know very little of image processing and even less of the terminology used, so please bear with me.

Basically, I want to merge two images together where one of t

相关标签:
4条回答
  • 2020-12-02 08:16

    http://www.imagemagick.org/Usage/compose/#dstin should do the trick, but you need to use images with alpha channels (that shouldn't be a problem).

    Edit: in PHP you have to pass it (imagick::COMPOSITE_DSTIN) as a parameter in compositeimage. Other filters in Composite Operator Constants may also be useful to you.

    0 讨论(0)
  • 2020-12-02 08:21

    This type of masking is exampled using a number of different techniques in ImageMagick Examples, Thumbnails, Mask and Paint http://www.imagemagick.org/Usage/thumbnails/#mask_paint

    Be warned however that the masking and the edges of the overlay image SHOULD NOT MATCH this is important or you may get problems with edge aliasing effects that is best avoided.

    Extracting an alpha mask of the ring, can be done using morphology operators to thin it down to a centerline can be used to generate a mask for any random 'ring' shape. ImageMagick Examples, Skeletons by Thinning, and Pruning http://www.imagemagick.org/Usage/morphology/#thinning_skeleton

    Anthony Thyssen Web Master for ImageMagick Examples, and Developer for ImageMagick

    PS: nice photo from Elfling

    0 讨论(0)
  • 2020-12-02 08:25

    Did you try this solution here as described by : https://stackoverflow.com/a/2351173/1093649 ?

    Run this in your server (with the right image names!), and let us know, thanks.

    nb : credits go to jspash

    0 讨论(0)
  • 2020-12-02 08:31

    So, finally, this should do what you need:

    Original image:

    http://i.stack.imgur.com/b7seR.png

    Opacity mask:

    enter image description here

    Overlay:

    http://i.stack.imgur.com/3ulkM.png

    Output:

    enter image description here

    The code:

    <?php
    $base = new Imagick('U0R4F.png');
    $mask = new Imagick('mask.png');
    $over = new Imagick('3ulkM.png');
    
    // Setting same size for all images
    $base->resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1);
    
    // Copy opacity mask
    $base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
    
    // Add overlay
    $base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);
    
    $base->writeImage('output.png');
    header("Content-Type: image/png");
    
    echo $base;
    ?>
    

    I hope it's right now! Note: In your example it looks like you downscaled the base image, which I didn't (my goal is just to show how the masking is done).

    0 讨论(0)
提交回复
热议问题