Message Boards Message Boards

2
|
5201 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Question about flattening images

Something I've wondered about a couple of times is how to "flatten" graphics in Mathematica. I don't know if this is really the right terminology, but the idea is that I have a bunch of semi-transparent objects inside a Graphics object. They are visually layered from bottom to top based on the order the appear inside Graphics, but I would like merge them down so that they're all in the same visual layer.

Here's an example:

Layered 9-gons on a blue background

Here's the code that produces the above image:

Module[{n, m, cols},
 n = 9;
 m = 20;
 cols = Insert[#, First[#], 4] &[
   RGBColor /@ {"#E45171", "#F8A79B", "#F8D99B", "#002C6A"}];
 Graphics[
  Table[{FaceForm[
     Directive[Blend[cols[[;; 4]], \[Theta]/(2 \[Pi])], Opacity[.3]]],
     EdgeForm[
     Directive[Blend[cols[[;; 4]], \[Theta]/(2 \[Pi])], 
      Thickness[.004]]], 
    Polygon[Table[{Cos[\[Theta]] + Cos[\[Phi] + \[Theta]], 
       Sin[\[Theta]] + Sin[\[Phi] + \[Theta]]}, {\[Phi], 0, 
       2 \[Pi] - 2 \[Pi]/n, 2 \[Pi]/n}]]}, {\[Theta], 0, 
    2 \[Pi] - 2 \[Pi]/m, 2 \[Pi]/m}], PlotRange -> 3, 
  Background -> cols[[5]], ImageSize -> 540]]

Now, the problem with the image is that the individual 9-gons are not all visually at the same level. For example, the last entry in the table is the reddish 9-gon just below 3 o'clock in the image which clearly lies above all the other 9-gons. The question is: how do I get the final image to appear flat?

I can more or less achieve this using ImageMultiply as follows:

Flattened 9-gon image with white background

imglist = Module[{n, m, cols},
   n = 9;
   m = 20;
   cols = 
    Insert[#, First[#], 4] &[
     RGBColor /@ {"#E45171", "#F8A79B", "#F8D99B", "#002C6A"}];
   Table[Graphics[{FaceForm[
       Directive[Blend[cols[[;; 4]], \[Theta]/(2 \[Pi])], 
        Opacity[.3]]], 
      EdgeForm[
       Directive[Blend[cols[[;; 4]], \[Theta]/(2 \[Pi])], 
        Thickness[.004]]], 
      Polygon[Table[{Cos[\[Theta]] + Cos[\[Phi] + \[Theta]], 
         Sin[\[Theta]] + Sin[\[Phi] + \[Theta]]}, {\[Phi], 0, 
         2 \[Pi] - 2 \[Pi]/n, 2 \[Pi]/n}]]}, PlotRange -> 3, 
     Background -> White, ImageSize -> 540], {\[Theta], 0, 
     2 \[Pi] - 2 \[Pi]/m, 2 \[Pi]/m}]];

Fold[ImageMultiply, First[#], #[[2 ;;]]] &[imglist]

(I used essentially the same trick to make Opposites Attract)

A minor problem is that ImageMultiply darkens all the colors (which could probably be gotten around by careful fiddling), but the big problem is that there's no way to do this trick with a colored or transparent background.

I assume there's some clever way to achieve this, so does anybody have any ideas?

2 Replies

The best solution I've come up with so far is to use Blend. Basically, I make a list where each entry in the table is a Graphics object with one missing 9-gon and a transparent background, apply Blend to the whole list, and then ImageCompose with a blank image with the background color I want (of course, I first just had a list with only one 9-gon in each entry, but this gets way too washed out when you apply Blend).

Here's the code:

 n = 9;
 m = 20;
 cols = Insert[#, First[#], 4] &[
   RGBColor /@ {"#E45171", "#F8A79B", "#F8D99B", "#002C6A"}];
 imglist = 
  Table[Graphics[
    Table[{FaceForm[
       Directive[Blend[cols[[;; 4]], Mod[\[Theta]/(2 \[Pi]), 1]], 
        Opacity[.3]]], 
      EdgeForm[
       Directive[Blend[cols[[;; 4]], Mod[\[Theta]/(2 \[Pi]), 1]], 
        Thickness[.004]]], 
      Polygon[Table[{Cos[\[Theta]] + Cos[\[Phi] + \[Theta]], 
         Sin[\[Theta]] + Sin[\[Phi] + \[Theta]]}, {\[Phi], 0, 
         2 \[Pi] - 2 \[Pi]/n, 2 \[Pi]/n}]]}, {\[Theta], 2 \[Pi] i/m, 
      2 \[Pi] (m + i - 2)/m, 2 \[Pi]/m}], PlotRange -> 3, 
    ImageSize -> 540, Background -> None], {i, 1, m}];
 ImageCompose[Graphics[Background -> cols[[5]], ImageSize -> 540], 
  Blend[imglist]]

And the resulting image:

Semitransparent 9-gons on a blue background

This seems like an annoyingly heavy-handed way of accomplishing this, so I'd still be very happy to hear about better solutions.

Posted 9 years ago
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract