2

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

Abuzer Firdousi
  • 1,552
  • 1
  • 10
  • 25

2 Answers2

3

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]\n" 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

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • that is great, thank you. any idea how to do this using PHP? all the relevant functions of these commends will be available in PHP? – Abuzer Firdousi Oct 29 '17 at 16:44
1

You can change the brightness by using -modulate brightness,saturation,hue when creating the tiled image. The defaults for no change are 100. So if you want to lower the brightness reduce it from 100. -modulate also allows you to change the saturation of the color and even the hue. It might also help if the pattern is tighter, more like yours. So I have changed the resize arguments.

So use the same commands as in my previous post, but change the last one as follows

Brightness=95

convert shirt_mod.png \
\( -clone 0 -resize 500% -tile pattern.png -draw "color 0,0 reset" -resize 20% -modulate 95,100,100 \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern1.png

enter image description here

Brightness=90

convert shirt_mod.png \
\( -clone 0 -resize 500% -tile pattern.png -draw "color 0,0 reset" -resize 20% -modulate 90,100,100 \) \
-compose hardlight -composite \
mask.png -alpha off -compose copy_opacity -composite \
shirt_pattern2.png

enter image description here

Adjust the resize and brightness as desired.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • WOW, -modulate 65,100,100 worked, Thanks Is there any way i can content you directly? I have few more question, and i guess you are the very right person for ask :) – Abuzer Firdousi Oct 29 '17 at 18:39
  • How can i generate following image https://www.suitposh.com/shirts/65_9ab1395ae1c4d012f36e4b5f90ec6cc5.jpg with https://suitposh.s3.amazonaws.com/images/products/57361-ebc9d0abc90ad228796e448e7e70430598ba279d-original.jpg using the same pattern – Abuzer Firdousi Oct 29 '17 at 19:05
  • 1
    You did not show any pattern image to use to put onto the shirt. See my web site for my direct contact information. http://www.fmwconcepts.com/imagemagick/index.html. If my answer helped you, please consider giving my second post an up-vote. – fmw42 Oct 29 '17 at 19:13
  • See also http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=20455 and http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=23348 – fmw42 Oct 29 '17 at 19:19
  • Using the same pattern https://i.stack.imgur.com/2UmGG.png or using https://ibb.co/iyyWD6 , I visited your site, did not find any contact form or contact information, – Abuzer Firdousi Oct 29 '17 at 19:22
  • My contact information is there down on the home page. But I see you finally found it, since I got your direct email. – fmw42 Oct 29 '17 at 19:35