3
$\begingroup$

I have two images:

enter image description here enter image description here

I can blend them:

img1 = Import["https://i.sstatic.net/1PJxM.png"];
img2 = Import["https://i.sstatic.net/blxtM.png"];
x = 0.5;
Blend[{img1, img2}, x]

Output:

enter image description here

Now how do I do the blending in such a way that "x" varies with image column? I want to get an image that is red on the left side, then transitions smoothly in the middle and is yellow on the right side

$\endgroup$

2 Answers 2

7
$\begingroup$

One way is:

img3 = ImageMultiply[img1, 
   LinearGradientImage[{White, Black}, ImageDimensions[img1]]];
img4 = ImageMultiply[img2, 
   LinearGradientImage[{Black, White}, ImageDimensions[img1]]];
ImageAdd[img4, img3]

enter image description here

$\endgroup$
4
  • $\begingroup$ That looks good. Now I just need a way to make it so that transition is more focused in the middle. Using image that is 1/3 black, 1/3gradient and 1/3 white instead of gradient should do it. Do you know how to accomplish this? $\endgroup$ Commented Nov 19, 2016 at 11:20
  • $\begingroup$ @AdamSikora I suggest you fiddle with the gradient lists yourself until you get what you want. You could do for instance: LinearGradientImage[{Red, White, Black}, ImageDimensions[img1] and LinearGradientImage[{Black, White, Yellow}, ImageDimensions[img1], and even make lists of different lengths. $\endgroup$
    – Feyre
    Commented Nov 19, 2016 at 11:23
  • $\begingroup$ Or make varying lists with the colors White,Gray,Black. $\endgroup$
    – Feyre
    Commented Nov 19, 2016 at 11:29
  • $\begingroup$ {White, White, White, Gray, Black, Black, Black} did the trick perfectly. Thanks Feyre $\endgroup$ Commented Nov 19, 2016 at 11:33
4
$\begingroup$

This is basically a custom version of Blend that takes a function, which can depend on x and y, that should return the fraction:

blend[{i1_, i2_}, f_] := ImageAdd[
  ImageApplyIndexed[adjust[f], i1],
  ImageApplyIndexed[adjust[1 - f[##] &], i2]
  ]

adjust[frac_][{r_, g_, b_, a___}, {y_, x_}] := Append[frac[x, y] {r, g, b}, a]

Here is how we can use it:

{w, h} = ImageDimensions[img1];    
blend[{img1, img2}, #/w &]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.