Apply overlay image but retain shadows

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?".
Post Reply
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Apply overlay image but retain shadows

Post by johanndm »

Hi,

Is there a way to apply an overlay image on base image and retain the shadows?

As example I would like to overlay a fabric onto a image with shadows as in the attached image.

I have tried to make the overlay transparent, but then the colour is not correct, as the base image colour then darkens the final image :-(

Any pointers or ideas will be appreciated.

Thanks
Johann

https://dev.zaxo.cz/362.png (base)
https://dev.zaxo.cz/kiwi.jpg (overlay)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply overlay image but retain shadows

Post by fmw42 »

try

Code: Select all

convert \( kiwi.jpg -crop 430x660+0+0 +repage \)  \
\( 362.png -alpha off -colorspace gray \)  \
-compose hardlight -composite \
\( 362.png -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result1.png
or

Code: Select all

convert \( kiwi.jpg -resize 430x660! \)  \
\( 362.png -alpha off -colorspace gray \)  \
-compose hardlight -composite \
\( 362.png -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result2.png
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Re: Apply overlay image but retain shadows

Post by johanndm »

Wow thanks - the second option works quite well - i.e. with the resize vs the crop. Thank you very much for the assistance and suggestions!!
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Re: Apply overlay image but retain shadows

Post by johanndm »

Hmm, Now I'm stuck. The above worked fine for most of the colours but now it seems to change the colour completely on lots of the samples I'm trying?

So for 362.png and kiwi.jpg it works fine, as well for coral-body.png and coral.jpg it works. But for pink-body.png and pink.jpg I get a very dark result?

I played around with the options - like changed colorspace to RGB but them my result is bright red?

Any pointers will be great :-)

Works:
convert \( coral.jpg -resize 430x660! \) \( coral-body.png -alpha off -colorspace gray \) -compose hardlight -composite \( coral-body.png -alpha extract \) -compose copy_opacity -composite new-coral.png
Image
Image
Image

Doesn't work:
convert \( pink.jpg -resize 430x660! \) \( pink-body.png -alpha off -colorspace gray \) -compose hardlight -composite \( pink-body.png -alpha extract \) -compose copy_opacity -composite new-pink.png
Image
Image
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply overlay image but retain shadows

Post by fmw42 »

Try this using -compose colorize: (unix syntax)

Code: Select all

convert pink-body.png \
\( +clone -alpha extract -write mpr:alpha +delete \) \
pink.jpg -alpha off -compose colorize -composite \
mpr:alpha -compose copy_opacity -composite result.png
Please always provide your IM version and platform, since syntax may vary.
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Re: Apply overlay image but retain shadows

Post by johanndm »

Hello,

Thanks once again for the advise - always amazed with all the options and power of ImageMagick.

I will have to play a little with the input image pink.jpg to make it slightly lighter so that the final image comes out the correct colour. Perhaps not a "clean" solution but I'm sure it will work as well.

Thanks
Johann
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Re: Apply overlay image but retain shadows

Post by johanndm »

Hello Fred,

I'm trying to do something very similar than before, except this time I'm using the same set of white base images and then overlaying a new colour photo for each new fabric - but I just don't seem to get it working :-) What would the optimal "recipe" be to make it work to overlay the colours on this base please.

Base images:
https://dev.zaxo.cz/im/base-1.png
https://dev.zaxo.cz/im/base-2.png

Overlay Photos:
https://dev.zaxo.cz/im/kiwi.jpg
https://dev.zaxo.cz/im/koralova.jpg
https://dev.zaxo.cz/im/seda.jpg
https://dev.zaxo.cz/im/pivonkova.jpg

I will keep experimenting but would be great if you could please give me some pointers please.

Kind regards,
Johann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply overlay image but retain shadows

Post by fmw42 »

try -compose multiply rather than -compose hardlight

Code: Select all

dims=`convert base-1.png -format "%wx%h" info:`
convert \( kiwi.jpg -resize $dims! \)  \
\( base-1.png -alpha off -colorspace gray \)  \
-compose multiply -composite \
\( base-1.png -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result1.png

dims=`convert base-2.png -format "%wx%h" info:`
convert \( kiwi.jpg -resize $dims! \)  \
\( base-2.png -alpha off -colorspace gray \)  \
-compose multiply -composite \
\( base-2.png -alpha extract \) \
-alpha off -compose copy_opacity -composite \
result2.png
johanndm
Posts: 9
Joined: 2017-12-03T06:58:02-07:00
Authentication code: 1152
Location: Czech Republic

Re: Apply overlay image but retain shadows

Post by johanndm »

:D :D :D Excellent - wow tried many other things but not "multiply"

Thanks once again for saving my bacon and helping - great great product and community!!
Post Reply