doubi6215 2017-10-28 17:19
浏览 254

在PHP中用图案图像填充图像的透明部分

I am trying to apply pattern on a transparent image with GD library.

I have an inverse transparent image and a couple of pattern images to apply on placeholder.

Any suggestion how to achieve this using GD library or image magic.

enter image description here

enter image description here

enter image description here

  • 写回答

2条回答 默认 最新

  • dongxie1907 2017-10-28 18:07
    关注

    I can tell you how to do it in ImageMagick Unix command line syntax. The basic idea is to use -compose hardlight between the shirt and the tiled pattern.

    First step is to trim off your ragged border around your pattern image. So I manually cropped it to look as follows so it could be tiled without the ragged borders showing:

    enter image description here

    When you use -compose hardlight, the effect is best if the average color of the image is mid gray (50%). So the first thing is to get the average gray level value and the difference from mid gray:

    mean=`convert shirt.png -scale 1x1! -alpha off -format "%[fx:100*mean]
    " info:`
    diff=`convert xc: -format "%[fx:50-$mean)]" info:`
    echo "mean=$mean; diff=$diff;"
    
    mean=86.9006; diff=-36.9006;
    

    Note: The above method gets the average gray level of only the opaque shirt and ignores areas that are transparent.

    Next, you need to remove the alpha channel and save it for later. Then modify the image without the alpha channel by adding back the required difference to get it to mean of mid gray. Then you need to modify the contrast to increase it so that the shading of the wrinkles show.

    convert shirt.png -alpha extract mask.png
    

    enter image description here

    convert shirt.png -alpha off -evaluate add $diff% -sigmoidal-contrast 10,50% shirt_mod.png
    

    enter image description here

    Then you tile out the pattern with a resizing to make the pattern more dense than in your original. And finally composite with hardlight and put the mask back into the alpha channel.

    convert shirt_mod.png \
    \( -clone 0 -resize 400% -tile pattern.png -draw "color 0,0 reset" -resize 25% \) \
    -compose hardlight -composite \
    mask.png -alpha off -compose copy_opacity -composite \
    shirt_pattern.png
    

    enter image description here

    Note that there are many ways to tile out the pattern to fill some image dimension. See http://www.imagemagick.org/Usage/canvas/#tile

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图