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

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]

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

Out:

Mathematica graphics Mathematica graphics

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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