Update: I put the two scripts up on GitHub, along with a nice demo GIF :)
Also did a bit of refactor and bug fixing. Check out the repo for the up-to-date scripts.

Each of the previous answers gets a piece of the puzzle. One changes transparency, but is static; the other dims when windows are inactive, rather than changes transparency.
To get the desired behavior (change transparency based on window focus), you need to write a script using iTerm2's Python API, and save it as an AutoLaunch script:
#!/usr/bin/env python3.7
import iterm2
# Store window change event reason objects in more convenient variables
window_selected = [iterm2.FocusUpdateWindowChanged.Reason.TERMINAL_WINDOW_BECAME_KEY]
async def update_tab_transparency(connection, tab, reason):
# Apply to any and all sessions in tab
for session in tab.sessions:
profile = await session.async_get_profile()
change = iterm2.LocalWriteOnlyProfile()
# For window change events bringing a window into focus,
# change transparency to zero
if reason in window_selected:
change.set_transparency(0)
# For window change events taking a window out of focus,
# make sure transparency matches the original profile
else:
# But, only need to do anything if the profile has been changed
# from the original
if profile.original_guid:
original = await iterm2.Profile.async_get(
connection, [profile.original_guid]
)
change.set_transparency(original[0].transparency)
await session.async_set_profile_properties(change)
async def main(connection):
app = await iterm2.async_get_app(connection)
# Initialize for first window
window = app.current_window
if window and window.current_tab:
tab = window.current_tab
reason = window_selected[0]
await update_tab_transparency(connection, tab, reason)
async with iterm2.FocusMonitor(connection) as mon:
while True:
# Block until a tab or window change
update = await mon.async_get_next_update()
# When switching to a new tab, treat as selecting a window
if update.selected_tab_changed:
tab = app.get_tab_by_id(update.selected_tab_changed.tab_id)
reason = window_selected[0]
# For window change events, use the provided event reason
elif update.window_changed:
window = app.get_window_by_id(update.window_changed.window_id)
tab = window.current_tab
reason = update.window_changed.event
# For other focus change events, do nothing
else:
continue
if tab:
await update_tab_transparency(connection, tab, reason)
iterm2.run_forever(main)
See the iTerm docs for details on using Python scripts
You want to set the desired transparency on your profile, and check the "Use transparency" box for "Settings for New Windows". This sounds counter-intuitive, but toggling that "Use transparency" setting doesn't seem to be possible via the API, so the script instead quickly switches windows to opaque on creation.
At least on my machine the window flickers briefly when first selected, but I'm not sure if there's anything I can do about that.
Note: macOS currently (11.6, Big Sur) treats windows the opposite of this—it makes active windows transparent (at least, certain parts of them) and inactive ones opaque (and faded). Try opening a new tab in Safari. It makes sense! The transparency creats a sense of depth. Seeing that there are windows behind the active one emphasizes that it's in front of them.
To get this behavior instead, use the following script. This variant is both simpler, and seems to run more smoothly (no flickering). It works best if you have the blur cranked to max, and transparency ~25.
#!/usr/bin/env python3.7
import iterm2
# Store window change event reason objects in more convenient variables
window_selected = [iterm2.FocusUpdateWindowChanged.Reason.TERMINAL_WINDOW_BECAME_KEY]
async def update_tab_transparency(connection, tab, reason):
# Apply to any and all sessions in tab
for session in tab.sessions:
profile = await session.async_get_profile()
change = iterm2.LocalWriteOnlyProfile()
# For window change events bringing a window into focus,
# make sure transparency matches the original profile
if reason in window_selected:
# But, only need to do anything if the profile has been changed
# from the original
if profile.original_guid:
original = await iterm2.Profile.async_get(
connection, [profile.original_guid]
)
change.set_transparency(original[0].transparency)
# For window change events taking a window out of focus,
# change transparency to zero
else:
change.set_transparency(0)
await session.async_set_profile_properties(change)
async def main(connection):
app = await iterm2.async_get_app(connection)
async with iterm2.FocusMonitor(connection) as mon:
while True:
# Block until a window change
update = await mon.async_get_next_update()
if update.window_changed:
window = app.get_window_by_id(update.window_changed.window_id)
tab = window.current_tab
if tab:
reason = update.window_changed.event
await update_tab_transparency(connection, tab, reason)
iterm2.run_forever(main)