Flag mashup generator
JavaScript (Node.js),  160  159 bytes
I/O format: matrix of 24-bit integers.
Takes input as (a)(b).

a=&gt;F=b=&gt;(g=a=&gt;[...new Set(a.flat())].sort(_=&gt;Math.random()-.5))(a).every((c,i)=&gt;c^=F[c]=B[i%B.length],B=g(b).slice(-g(a).length))?a.map(r=&gt;r.map(c=&gt;F[c])):F(b)

Try it online!
Commented
Helper function
The helper function \$g\$ takes a matrix as input and returns a shuffled list of the distinct values it holds.
g = a =&gt;
  [...new Set(a.flat())]
  .sort(_ =&gt; Math.random() - .5)

Main code
a =&gt; F = b =&gt;                 // a[] = first image, b[] = second image
  g(a).every((c, i) =&gt;        // for each color c at position i in the list of shuffled
                              // colors of the first image:
    c ^=                      //   make sure that c is different from the
      F[c] = B[i % B.length], //     substitute color F[c] picked from the second image
    B =                       //   define B[] as:
      g(b)                    //     the list of shuffled colors from the second image
      .slice(-g(a).length)    //     resized to the number of colors in the first image
  ) ?                         // end of every(); if truthy:
    a.map(r =&gt;                //   return a[] with each original color
      r.map(c =&gt; F[c])        //   replaced with its substitute color
    )                         //
  :                           // else:
    F(b)                      //   try again