4
$\begingroup$

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

Original Image

Image converted to purple

Image with white converted to purple

Attempt at background subtraction

Attempt at Background subtraction

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

2 Answers 2

7
$\begingroup$

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

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

Mathematica graphics

$\endgroup$
4
$\begingroup$

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.sstatic.net/zUpTv.png"];
(*Method 1*)
img // Rasterize // ImageData // Map[Last@# &, #, {2}] & // ArrayPlot
(*Method 2*)
img // Rasterize // ColorNegate

Out:

Mathematica graphics Mathematica graphics

$\endgroup$
1
  • $\begingroup$ How could I get the purple effect here though? $\endgroup$ Commented Jun 3, 2017 at 23:32

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.