How to paste one image to another one, by forcing the former's background as transparent?

H42 07/11/2018. 4 answers, 201 views
image-processing image

I have 2 images, imgBlackOnly and imgRedOnly as below, which already have the same ImageDimensions.

enter image description here

enter image description here

I want to paste imgBlackOnly to cover imgRedOnly, while the white part of imgBlackOnly is treated as "transparent" as what we did in the tiny software Paint of Windows. Thus my desired output is imgBlackCoverRed as below:

enter image description here

But one point to note is, the white part of imgBlackOnly is not totally white (since it is a screen capture from somewhere else), and therefore we need to force some "nearly white" part to be "totally white" by adding a manual threshold.

How can I do that? Many thanks!


To avoid misunderstanding, I also want to point out that the original picture of imgBlackOnly is not just black. It has several colors, with a "nearly white" background. Thanks.

4 Answers


C. E. 07/11/2018.

The definition of the function ColorReplace[img, color] is that it takes colors that are similar to (not exactly the same as) color and replaces those pixels with transparent pixels.

i1 = Import["https://i.stack.imgur.com/wOPPys.png"];
i2 = Import["https://i.stack.imgur.com/KdDgSs.png"];
i1 = ColorReplace[i1, White];
i2 = ColorReplace[i2, White];
ImageCompose[i1, i2]

Mathematica graphics

You may notice that there are still some white pixels around the black figure. We can fix that by providing our own threshold for how far away a color can be from white and still be considered similar:

i2 = ColorReplace[i2, White, 0.5];
ImageCompose[i1, i2] 

Mathematica graphics


andre 07/11/2018.
imgBlack=Import["https://i.stack.imgur.com/KdDgSs.png"];
imgRed=Import["https://i.stack.imgur.com/wOPPys.png"];
ImageCompose[imgRed,RemoveBackground[imgBlack]]  

enter image description here


rhermans 07/11/2018.

The threshold mentioned by @C.E. can also be used with RemoveBackground to remove the white edge.

Module[
 {img1, img2},
 {img1, img2} = Import /@ {
    "https://i.stack.imgur.com/KdDgSs.png",
    "https://i.stack.imgur.com/wOPPys.png"};
 ImageCompose[
  img2,
  RemoveBackground[img1, {White, 0.5}]]
 ]

Mathematica graphics


Fortsaint 07/12/2018.

cover=#2~ImageCompose~SetAlphaChannel[#1, ColorNegate@#1] &

cover[imgBlackOnly,imgRedOnly]


HighResolutionMusic.com - Download Hi-Res Songs

1 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
2 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
3 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
4 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
5 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
6 BTS

Waste It On Me flac

BTS. 2018. Writer: Steve Aoki;Jeff Halavacs;Ryan Ogren;Michael Gazzo;Nate Cyphert;Sean Foreman;RM.
7 Imagine Dragons

Bad Liar flac

Imagine Dragons. 2018. Writer: Jorgen Odegard;Daniel Platzman;Ben McKee;Wayne Sermon;Aja Volkman;Dan Reynolds.
8 Nicki Minaj

No Candle No Light flac

Nicki Minaj. 2018. Writer: Denisia “Blu June” Andrews;Kathryn Ostenberg;Brittany "Chi" Coney;Brian Lee;TJ Routon;Tushar Apte;ZAYN;Nicki Minaj.
9 Brooks

Limbo flac

Brooks. 2018.
10 BlackPink

Kiss And Make Up flac

BlackPink. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
11 Diplo

Close To Me flac

Diplo. 2018. Writer: Ellie Goulding;Savan Kotecha;Peter Svensson;Ilya;Swae Lee;Diplo.
12 Rita Ora

Velvet Rope flac

Rita Ora. 2018.
13 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
14 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
15 Billie Eilish

When The Party's Over flac

Billie Eilish. 2018. Writer: Billie Eilish;FINNEAS.
16 K-391

Mystery flac

K-391. 2018.
17 Cher Lloyd

None Of My Business flac

Cher Lloyd. 2018. Writer: ​iamBADDLUCK;Alexsej Vlasenko;Kate Morgan;Henrik Meinke;Jonas Kalisch;Jeremy Chacon.
18 Backstreet Boys

Chances flac

Backstreet Boys. 2018.
19 Lil Pump

Arms Around You flac

Lil Pump. 2018. Writer: Rio Santana;Lil Pump;Edgar Barrera;Mally Mall;Jon Fx;Skrillex;Maluma;Swae Lee;XXXTENTACION.
20 Kelly Clarkson

Never Enough flac

Kelly Clarkson. 2018. Writer: Benj Pasek;Justin Paul.

Related questions

Hot questions

Language

Popular Tags