Question: I have an SVG image with many objects, stacked. What I need is a final SVG, with the same visual result, but in a way that I could change the opacity of any object without revealing the hidden parts of underlying objects, revealing just the "hole" behind.

I've done [this diagram](http://cl.ly/image/1G0k2f1w2k11) to illustrate the request:
![Diagram example image](https://i.stack.imgur.com/UYmDg.png)

This example above was done by hand with Inkscape, selecting every square and duplicating it as many times as the number of differences (`ctrl` + `-`) I needed to "cut" the underlying objects. This method is very hard to apply with a large amount of objects.

Any software (Adobe Illustrator, command line tool...) or method that solves this problem will be welcome.

Answer: 
On Illustrator:

**If there's no transparency...**
---------------------------------

...select the shapes, then use the `'Trim'` button in the `Pathfinder` window.

![enter image description here](https://i.stack.imgur.com/ZJNDu.png)

Trim can act strangely with objects that have transparency, so...

**If there *is* transparency...**
---------------------------------

...select the squares, then `Object > Flatten Transparency`, set 'vectors' to 100%.

Image illustrating it from [this other question](https://graphicdesign.stackexchange.com/questions/6631/how-to-sample-actual-color-of-an-area-in-illustrator):

![enter image description here](https://i.stack.imgur.com/QPx9e.gif)

Sometimes, `Flatten Transparency` leaves the bottom object uncut, sitting under everything else. If this happens, then since after `Flatten Transparency` all your vector shapes have opacity 100%, you can just select that bottom shape and the shapes that overlap it and use `Trim` as above.

Then save as SVG, if you need SVG. To avoid the coincident edges problem Ilmari Karonen describes, you could use `Object > Transform > Transform Each` to scale each object to say 100.5% around its own centre. Or, you could give each object a very thin stroke of its own fill colour (probably a better approach since ideally the tiny bit of overlap should be the same for everything, not proportional to size).
I have an idea, though i'm sure there is a better way.

Using Inkscape

1. Place your objects and make sure to set each object's color where none of the same color are touching, (use as few as colors as possible) you will change these later.
2. `Edit -> Preferences` near the bottom of the selection, select `Bitmaps` set `Resolution (256)` and close.
3. select all your objects, then `Edit -> Make a Bitmap copy` (it will take a bit of time generating the image).
4. `Path -> Trace Bitmap` in the `Mode tap` set `Multiple scan: Scans (6, one for each color used plus one for background), Colors (on)` mark only `Remove background`.
5. In the `Options tab` un-mark everything! Submit `OK`.
6. `Right click` on group, select `Ungroup`.
7. Now select all your Objects, and `Path -> Break Apart`.
8. And now you can change your colors to look pretty!

You may need to delete some stray lines.

Be sure to set the `Resolution` back!. And let me know if it works for you.