Can imagemagick do photoshop-like blend-if effect?

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
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Can imagemagick do photoshop-like blend-if effect?

Post by sssddd »

See this for the effect that I would like to achieve using imagemagick: http://www.photoshopessentials.com/phot ... /blend-if/

From what I understand, basically photoshop does it by blending colors of the two images on regions where they overlap, and it can specify the colors in each image that need to be modified and specify the extent of each color to use from each image in order to generate a gradually-transformed color. I have read the IM examples and Fred's scripts, but still couldn't find something that's exactly what I want. Is it possible to do this?

As reference, my imagemagick Version: ImageMagick 6.5.7-8 2010-12-02 Q16
on linux, running IM by calling shell command from php exec()

Many thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can imagemagick do photoshop-like blend-if effect?

Post by fmw42 »

I don't know PS that well, but it looks like it is just a matter of adding some thresholded random noise to your white on black text mask before compositing the image with white. See compositing with a mask image at http://www.imagemagick.org/Usage/compose/#compose and http://www.imagemagick.org/Usage/compose/#mask.


Here is one way (perhaps not the most efficient, but it makes it easy to demostrate):

original:
Image

The following makes a random noise image and thresholds it at 50%. Then it makes a white on black text image. Then it composites the two by multiplication to have textured white text on black background.

convert \( -size 256x256 xc: +noise random -colorspace gray -threshold 50% \) \
\( -size 256x256 -background black -fill white -gravity center +antialias \
-pointsize 72 -font /library/fonts/verdana.ttf caption:"THIS IS IM" \) \
-compose multiply -composite brick_mask.gif

Image

The following puts the mask image (negated) into the alpha channel of the brick image, then flattens that onto a white background.
convert bricks_tilable2.jpg \( brick_mask.gif -negate \) \
-alpha off -compose copy_opacity -composite \
-compose over -background white -flatten brick_result.gif

Image
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Re: Can imagemagick do photoshop-like blend-if effect?

Post by sssddd »

Thanks for the reply. However, I think it's more of a regulated distortion of the text, not some random noise. Note how the background image has different shades and colors. These would affect how the text is blended. I'm not too sure exactly what PS is doing underneath, but it seems it's converting part of the text to transparent or semi transparent according to the color/shade of the overlapping background image, to create a gradual "blend" effect with the background... or am I missing something?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can imagemagick do photoshop-like blend-if effect?

Post by fmw42 »

then in place of the random image, just threshold the background image that makes the conditional for mottling only where darker than some threshold from the original brick image


convert \( bricks_tilable2.jpg -colorspace gray -threshold 40% \) \
\( -size 256x256 -background black -fill white -gravity center +antialias \
-pointsize 72 -font /library/fonts/verdana.ttf caption:"THIS IS IM" \) \
-compose multiply -composite brick_mask2.gif

Image

convert bricks_tilable2.jpg \( brick_mask.gif -negate \) \
-alpha off -compose copy_opacity -composite \
-compose over -background white -flatten brick_result2.gif

Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Can imagemagick do photoshop-like blend-if effect?

Post by anthony »

If you can find the exact formula behind blend-if I am certain we can add it!

however have you tried using the mask as a lighting effect (Black may need to be set to pure gray).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sssddd
Posts: 14
Joined: 2011-02-15T03:16:59-07:00
Authentication code: 8675308

Re: Can imagemagick do photoshop-like blend-if effect?

Post by sssddd »

Thanks for the input.

I believe PS does this by controlling pixels with certain color/shade on different layers/images. Look at the following sentence (I quoted from the other site):
"Moving the black and white sliders for the bar on top will cause areas of the currently selected layer to disappear from view. Moving the sliders for the bar on the bottom will cause areas of the layer(s) below the currently selected layer to show through the selected layer, as if it's punching holes through the layer. In other words, since I had my type layer selected, if I move the sliders on the top bar in towards the center, I'll be making areas in my type disappear. If I move the bottom bar's sliders in towards the center, I'll be making areas of my brick wall photo show through my type. Moving the white slider affects the lightest areas in the image, and moving the black slider affects the darkest areas. The further I move the sliders, the greater the tonal range that's affected.".
...
and a single slider point (similar to Fred's threshold) has the problem of "The areas where the brick wall is showing through the type are too harsh. It's either the text showing 100% or the wall behind it showing 100%". To make "a subtle transition between the two to smooth things out and make it look more realistic ...", the slider can be split to two points, and "With the black slider now split in two, the half on the left is where the blending begins, the half on the right is where the blending reaches 100%, and the area in between is the transition area. I can now drag both halves independently of each other".

So my understanding is, the background image is considered as grayscale, and there are two thresholds for black-like pixels, the pixels below the lower threshold and above the upper threshold are not changed, the pixels between the range are changed, with a smooth/gradual blend with the top layer (maybe -compose multiply?) And changes to white-like pixels can be done in the same way, while the changes to both black and white can be done independently/simultaneously.

Does that make sense?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Can imagemagick do photoshop-like blend-if effect?

Post by anthony »

sssddd wrote:Does that make sense?
Some what though as a person without Photoshop I can only imagine what the sliders actually do.
Basically I can only make a guess! -- mathematically.

if you have something more definitive, that would be better.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Can imagemagick do photoshop-like blend-if effect?

Post by fmw42 »

If you need a black and white threshold, that is easy just change -threshold XX% to

-black-threshold XX% -white-threshold YY%

anything at or below XX% becomes black, anything above YY% becomes white, and everything between is a linear shade of gray from black to white.

Putting the mask into the alpha channel as I did, allows for 8-bit masking (transparency), thus allowing a blend of the background and the white.
Post Reply