<span class="mw-page-title-namespace">Blog</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Hacks/Changing widths andor opacity of Inkscape with a python script</span>
Fabrice P. Lauss𝕪's Web

Our paper with Daniel on boson correlations being spurious[1] has a figure which, to my taste, features too small widths:

This can be changed with a python script as follows:

#!/usr/bin/env python3

import re
import sys

# Get input filename from command line or use default
if len(sys.argv) > 1:
    input_file = sys.argv[1]
else:
    input_file = "fig1.svg"

output_file = input_file.replace(".svg", "_thicker.svg")

# Read the SVG
with open(input_file, 'r', encoding='utf-8') as f:
    content = f.read()

# Function to increase stroke-width by 15%
def increase_stroke_width(match):
    full_style = match.group(0)
    
    # Extract current width
    width_match = re.search(r'stroke-width:([\d.]+)', full_style)
    if width_match:
        old_width = float(width_match.group(1))
        new_width = round(old_width * 2.5, 5)   # 2.5 times thicker
        # Replace the width value
        return full_style.replace(f'stroke-width:{old_width}', f'stroke-width:{new_width}')
    return full_style

# Match lines that are EITHER orange OR blue
pattern = r'fill:none;stroke:#(?:fd7f00|0000fd|00a800);stroke-width:[\d.]+(?:;[^"]*?)?'

content = re.sub(pattern, increase_stroke_width, content, flags=re.IGNORECASE)

# Save modified file
with open(output_file, 'w', encoding='utf-8') as f:
    f.write(content)

print(f"✅ Done! Increased stroke width by 15% for orange AND blue lines")
print(f"   Original: {input_file}")
print(f"   Modified: {output_file}")

The code to change opacity of the bargraphs, which I also found too opaque:

#!/usr/bin/env python3

import re
import sys

if len(sys.argv) > 1:
    input_file = sys.argv[1]
else:
    input_file = "fig1.svg"

output_file = input_file.replace(".svg", "_opacity.svg")

with open(input_file, 'r', encoding='utf-8') as f:
    content = f.read()

def set_opacity(match):
    style = match.group(0)
    # Change both fill-opacity and stroke-opacity to 0.25
    style = re.sub(r'fill-opacity:[\d.]+', 'fill-opacity:0.25', style)
    style = re.sub(r'stroke-opacity:[\d.]+', 'stroke-opacity:0.25', style)
    return style

# Much more restrictive pattern - must have both fill and stroke with same color + fill-rule:nonzero
pattern = r'fill:#(?:fd7f00|0000fd);fill-opacity:[\d.]+;fill-rule:nonzero;stroke:#(?:fd7f00|0000fd);stroke-width:[\d.]+;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:3\.25;stroke-opacity:[\d.]+'

content = re.sub(pattern, set_opacity, content, flags=re.IGNORECASE)

with open(output_file, 'w', encoding='utf-8') as f:
    f.write(content)

print(f"✅ Done! Opacity changed to 0.25 (very restrictive version)")
print(f"   Only applied to elements matching your exact style pattern")
print(f"   Original: {input_file}")
print(f"   Modified: {output_file}")

Then the comparison can be made with png exports:

for f in *.svg; do inkscape --export-background=white --export-background-opacity=1 --export-filename="${f%.svg}.png" "$f"; done

References

  1. Boson correlations from classical states are spurious. Daniel Salazar and Fabrice Laussy. Not yet online! (2026)