Here is a better example that includes displacing the pattern.
1 ) flatten the hoodie image, since it has an alpha channel that defines the color not the mask
2 ) use flood fill to make a mask image separating the hoodie from the background area
3 ) put the mask into the alpha channel and get the average grayscale of the opaque region
4 ) get a difference value for making the lighting image and for the displacement image
5 ) resize the pattern and tile it out to the size of the hoodie image
6 ) apply the lighting difference to the clothing image to make the average grayscale at 40% (adjust as desired for brightness)
7 ) apply the displace difference to the clothing image to make the average grayscale at 50%, enhance the contrast and make the background area transparent 50% gray so that it does not get displaced in the background area
8 ) combine the tiled_pattern image with the lighting image using hard light composite then apply the displacement image to distort the pattern on the hoodie.
Code: Select all
convert mens_hoodie_back.png -flatten -colorspace gray clothing.png
Code: Select all
convert clothing.png -bordercolor white -border 1 -fuzz 0.2% -fill black -draw "color 0,0 floodfill" \
-alpha off -fill white +opaque black -blur 0x1 -level 50x100% -shave 1x1 mask.png
Code: Select all
color=`convert clothing.png mask.png -alpha off -compose copy_opacity -composite \
-scale 1x1! -alpha off -type grayscale -format "%[pixel:u.p{0,0}]" info: | tr -cs "0-9*\n" " "`
diffl=`convert xc: -format "%[fx:(40-100*($color/255))]" info:`
diffd=`convert xc: -format "%[fx:(50-100*($color/255))]" info:`
echo "color=$color; diffl=$diffl; diffd=$diffd"
color= 227 ; diffl=-49.0196; diffd=-39.0196
Code: Select all
convert clothing.png \( 0344796.jpg -resize 10% +write mpr:tile +delete \) \
-tile mpr:tile -draw "color 0,0 reset" tiled_pattern.png
Code: Select all
convert clothing.png -evaluate add $diffl% lighting.png
Code: Select all
convert \( clothing.png -evaluate add $diffd% -sigmoidal-contrast 5x50% \) \
mask.png -alpha off -compose copy_opacity -composite \
-background "gray(50%)" -alpha background displace.png
Code: Select all
convert tiled_pattern.png lighting.png -compose hardlight -composite \
displace.png -define compose:args=5x5 -compose displace -composite new_result.png
