8
$\begingroup$

I am attempting to generate partially transparent images for PNG Export, but seem to run to the following issue. If I Rasterize a simple piece of Graphics with Background -> None (transparent) it looks worse:

rings = Image[
    Rasterize[
     Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}],
       Background -> #, RasterSize -> 400, ImageSize -> 400]] & /@ {None, White}

enter image description here

First version appears jagged, as a circle drawn using a too simple polygon would do. I can verify this with ImageDifference:

ImageAdjust[ImageDifference @@ (RemoveAlphaChannel[#, White] & /@ rings)]

enter image description here

Jagged pattern is clearly visible.

How to use Rasterize with Background -> None (or anything similar generating an alpha-channel image) and achieve good output quality without resorting to generating primitives such as Disk using hand-crafted code?

Clarification:

I want alpha channel on the output to behave as it does on Rasterize[..., Background -> None]. That is, the image having conceptually three regions: transparent background, black outer disk and white (non-transparent) inner disk.

(These screenshots were taken on Mathematica 9.0.1.0 running on OS X 10.8.4.)

$\endgroup$
4
  • $\begingroup$ I could not reproduce your example in v7 on Windows, FWIW. $\endgroup$
    – Mr.Wizard
    Commented Sep 9, 2013 at 20:20
  • $\begingroup$ @Mr.Wizard I hope someone can try to reproduce this on v9. These things tend to be particularly dependent of graphics hardware and drivers, but it would really seem like Mma just uses a poor polygon... unless that's some sort of failed attempt to render a spline. $\endgroup$
    – kirma
    Commented Sep 9, 2013 at 20:30
  • 1
    $\begingroup$ I see it in v9 on Windows, but it works as expected if I set the Background option in Graphics instead of Rasterize. $\endgroup$ Commented Sep 10, 2013 at 4:56
  • $\begingroup$ @MichaelHale Thanks for confirming this issue! The sad part about Background -> None in Graphics is that it alone is not enough to produce an alpha-channel image, and if the same is done also in Rasterize, the issue creeps up again. $\endgroup$
    – kirma
    Commented Sep 10, 2013 at 5:24

3 Answers 3

4
$\begingroup$

Another easy idea: use difference between white and black backgrounds to set alpha channel

SetAlphaChannel[#2, ColorNegate@ImageSubtract[##]] & @@ (
 Rasterize[
    Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}],
    Background -> #, RasterSize -> 400, 
    ImageSize -> 400] & /@ {Black, White})
$\endgroup$
5
  • $\begingroup$ If you don't mind my asking: what is your background with Mathematica? You are new to our site but quite clearly not new to Mathematica. Thanks for posting lots of good answers. $\endgroup$
    – Mr.Wizard
    Commented Sep 10, 2013 at 10:49
  • $\begingroup$ Heh. Considering confusing multitude of color specifications in Mathematica, this is probably easiest way to accomplish the task at hand, even if not the most conceptually puritan. $\endgroup$
    – kirma
    Commented Sep 10, 2013 at 12:23
  • $\begingroup$ @Mr.Wizard I'm glad you enjoyed my answers. I started to use Mathematica intensively one year ago when I realized that it is suitable for big numerical calculations. My scientific interests are conductivity and thermal conductivity of disordered media, random matrix theory, and related topics (may be I intersect with the scientific area of Leonid Shifrin). $\endgroup$
    – ybeltukov
    Commented Sep 10, 2013 at 22:50
  • 1
    $\begingroup$ @Mr.Wizard For numerical analysis of eigenvalue distributions, Green functions, etc. I have implemented The Kernel Polynomial Method and have got a great experience in Mathematica. For example, this this method can very fast plot something like SmoothHistogram@Eigenvalues of large sparse matrices. If anybody wants I can tell more about it. $\endgroup$
    – ybeltukov
    Commented Sep 10, 2013 at 22:50
  • $\begingroup$ This works, although when you have smooth transparency transitions I found it's better to use this modification: SetAlphaChannel[#2, ColorNegate@ImageAdjust@ImageSubtract[##]] & @@ (Rasterize[object, Background -> #, RasterSize -> size, ImageSize -> size] & /@ {White, GrayLevel[.85]}) $\endgroup$
    – shrx
    Commented Jul 6, 2017 at 11:50
1
$\begingroup$

Oops. I was too tired to fully verify the expected output before I wrote my comment. Here is a simple hack to make the image you want though.

disk[c_, r_] := 
 Polygon@Table[c + r {Cos@a, Sin@a}, {a, 0, 2 Pi, Pi/50}]
Image[Rasterize[
  Graphics[{Black, disk[{0, 0}, 1], White, 
    disk[{1, 1}/78, 1 - Sqrt[2]/64]}], Background -> None, 
  RasterSize -> 400, ImageSize -> 400]]
$\endgroup$
1
  • $\begingroup$ Yes, this fixes the issue, but it's a trick I wanted to avoid and already referred on my original question as "without resorting to generating primitives such as Disk using hand-crafted code". What if similar issues come up with less obvious graphics primitives? That's the problem I'm pondering about. Nonetheless, +1. $\endgroup$
    – kirma
    Commented Sep 10, 2013 at 7:55
1
$\begingroup$

One easy idea is to set the alpha channel directly but whether this satisfy your needs really depends on what exactly you want to have. Btw, it is worth that you use ColorSeparate to see for yourself what exactly the output in the single channels of your jagged image is; you may be surprised.

SetAlphaChannel[#, ColorNegate[#]] &@
 Rasterize[
  Graphics[{Black, Disk[], White, Disk[{1, 1}/64, 1 - Sqrt[2]/64]}], 
  Background -> White, RasterSize -> 400, ImageSize -> 400]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Conceptually, I'd want area surrounding the outer circle to be transparent, but inner white circle not to be. Surely, this can be achieved by painting alpha channel separately and combining it to color channels. This far, it'd seem there is a bug, and the best that can be achieved is a workaround. $\endgroup$
    – kirma
    Commented Sep 10, 2013 at 8:01

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.