Applying Logo To Shirt (white halo)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying Logo To Shirt (white halo)

Post by fmw42 »

Here is how I would do it step by step. You can combine these into one command if you want.


# create shirt mask

Code: Select all

convert shirt.jpg -threshold 40% -negate shirt_mask.png
Image

# insert picture in transparent background at correct location for placement on shirt

Code: Select all

convert \( shirt.jpg -alpha transparent  \) picture.png -geometry +313+192 -composite picture_trans.png
Image

# extract picture mask

Code: Select all

convert picture_trans.png -alpha extract picture_mask.png
Image

# apply shirt mask to shirt and get mean of opaque only area

Code: Select all

convert shirt.jpg shirt_mask.png -alpha off -compose copy_opacity -composite -scale 1x1!  -format "%[fx:mean]" info:
0.206912
# process tshirt to make sure shirt area is close to mean=0.5 and background is mid gray

Code: Select all

convert shirt.jpg shirt_mask.png -alpha off -compose copy_opacity -composite -evaluate add 30% \
-alpha on -background "gray(50%)" -alpha background -alpha off tshirt_process.png
Image

# create lighting mask --
# you can include some kind of contrast adjustment here if you want to make the lighting effect stronger,
# but keep the mean close to 0.5 (e.g. -level 10x90% or -sigmoidal-contrast 2x50%)
# also add blur if you want less texture to show from shirt in the picture

Code: Select all

convert tshirt_process.png lighting.png
Image

# create displacement map -- blur to make it smooth so texture does not cause jagged offsets

Code: Select all

convert tshirt_process.png -blur 0x2 displacement.png
Image

# apply lighting to picture using -compose hardlight and put back alpha channel

Code: Select all

convert picture_trans.png lighting.png -compose hardlight -composite \
picture_mask.png -compose over -compose copy_opacity -composite picture_light.png
Image

# apply displacement map to lighted picture

Code: Select all

convert picture_light.png displacement.png -define compose:args=-20,-20 \
-compose over -compose displace -composite picture_light_displace.png
Image

# apply displaced picture over tshirt and apply shirt mask

Code: Select all

convert shirt.jpg picture_light_displace.png -compose over -composite \
shirt_mask.png -alpha off -compose copy_opacity -composite shirt_picture.png
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Applying Logo To Shirt (white halo)

Post by Bonzo »

Thank you both for the help and I have both methods working although snibgo's is still a bat file at the moment.
We are exagerating the difference between shadows and hightlights of a black shirt. This is crazy. A white shirt would be better.
The shirts will be of different colours but I thought it would be a good idea to get the code working first; although I now see that will probably cause problems.

I will be working with a larger file size photo in the first place but I need to reduce the file size to shorten process time. I suppose saving the reduced image file as a miff file would be the best method. I will try adding a timer to see what the process time actually is.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying Logo To Shirt (white halo)

Post by fmw42 »

The shirts will be of different colours but I thought it would be a good idea to get the code working first; although I now see that will probably cause problems.

I will be working with a larger file size photo in the first place but I need to reduce the file size to shorten process time. I suppose saving the reduced image file as a miff file would be the best method. I will try adding a timer to see what the process time actually is.

When I have done this for multiple colored Tshirts, I worked with a white shirt to get the masks, lighting and displacement maps. Then just replaced the actual shirt in the final step. But you provided a black shirt to start and I was concerned to negate it since the shading would be backwards. So in general, I would start with a white shirt for setting up all the images, if you have that option.

To speed up the processing, in the past, I made my commands into a single command line after getting the mask of the tshirt and the lighting and displacement maps. Save them as miff (or mpc). Then I put all the rest of the processing into a single command using mpr to save intermediate images as needed. This way the same tshirt mask and maps can be used for any colored Tshirt of the same geometry and for any image. The image may need to be resized and placed accordingly, but I expect that information will come from the front end system.

Note: if you have an image that has transparency (like text on transparent background), then you may need to modify the process to combine alpha channels to preserve the transparency in the image.
MrLimon15
Posts: 2
Joined: 2018-10-18T08:59:55-07:00
Authentication code: 1152

Re: Applying Logo To Shirt (white halo)

Post by MrLimon15 »

fmw42 wrote: 2014-05-19T12:00:38-07:00 When I have done this for multiple colored Tshirts, I worked with a white shirt to get the masks, lighting and displacement maps. Then just replaced the actual shirt in the final step.
Which parameters would you use to get the masks, lighting and displacement maps for a white image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Applying Logo To Shirt (white halo)

Post by fmw42 »

I do not understand the question. I show above step by step how to process the tshirt to get the lighting and displacement maps.

Post your tshirt image and the image you want to put on it.

Better, just use my tshirt script at my link below. You will need a unix environment, which could be Linux, Mac OSX, Window 10 unix, or Windows with Cygwin
Post Reply