# Colorizing a gray scale image

I would like to modify a gray scale image so the black background turns transparent and the white pixels go to a purple color. I attached the image, my attempt to turn the white pixels in the image to purple without changing the black background, and my attempt to change the background from black to white.

It seems like when I try to remove the black background and convert it to white, the end image looks very choppy. It seems like I can't maintain the "wispyness" of the original grayscale image and the purple to white cutoff is harsher. I tried using some GaussianFilters to smooth out the transition but would really like to just convert the original image properly so that filters are not necessary as they are not having the effect I want

Code to convert to purple:

(*imD is original grayscale image matrix*)
Colorize[Image[imD],
ColorFunction -> Function[gray, RGBColor[93/255 gray, 0, (gray)]]]


Code to convert to white background:

ColorReplace[
Colorize[Image[imD, ImageSize -> Scaled[0.2]],
ColorFunction -> Function[gray, RGBColor[93/255 gray, 0, gray]]],
Black -> White]


Original image

Image converted to purple

Attempt at background subtraction

• How precise do you need it to cleave to the original image? Is this just an effect? – b3m2a1 Jun 3 '17 at 7:25
• Replace RGBColor[93/255 gray, 0,(gray)] with RGBColor[93/255, 0, 1, gray]. – Rahul Jun 3 '17 at 7:49
• wow phenomenal, that works perfect. Thank you so much! – user2476576 Jun 3 '17 at 23:27

The solution is quite simple, although there seems to be a misunderstanding about what exactly purple is:

img = Import["https://i.stack.imgur.com/zUpTv.png"];
SetAlphaChannel[ImageApply[#*{1/2, 0, 1/2} &, img], img]


It's better to use alpha parameter. The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque)

In:

img = Import["https://i.stack.imgur.com/zUpTv.png"];
(*Method 1*)
img // Rasterize // ImageData // Map[Last@# &, #, {2}] & // ArrayPlot
(*Method 2*)
img // Rasterize // ColorNegate


Out:

• How could I get the purple effect here though? – user2476576 Jun 3 '17 at 23:32