You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

能否通过Python将SVG图标叠加于JPG图像之上并输出仅保留矢量图标的SVG文件

Can I create an SVG with only overlaid SVG icons (no JPG background) and do this in Python?

Absolutely, this is totally achievable—and Python is a fantastic tool to make it happen! Let’s break down the solution clearly:

Core Idea

Since you don’t need to retain the JPG background at all, your task simplifies to combining multiple 48×48 SVG icons into a single SVG file, positioned exactly where you intended them to be over the original JPG. You don’t even need to process the JPG’s pixel data—you just need its dimensions to set the canvas size of your final SVG, ensuring icon positions match your original layout.

Python Implementation (Using svgutils)

The easiest way to do this is with the svgutils library, which is purpose-built for combining and manipulating SVG files. Here’s a step-by-step guide:

1. Install the Library

First, install svgutils via pip:

pip install svgutils

2. Sample Code

This script will take your icon paths and their target positions, then generate a pure vector SVG with just the icons:

from svgutils.compose import SVG, Figure

# Step 1: Define the dimensions of your original JPG (to match the canvas size)
original_jpg_width = 1920  # Replace with your JPG's actual width
original_jpg_height = 1080  # Replace with your JPG's actual height

# Step 2: List your SVG icons with their target (x, y) positions (top-left corner coordinates)
# Format: ("path/to/icon.svg", x_position, y_position)
icon_positions = [
    ("icon_home.svg", 150, 200),
    ("icon_settings.svg", 800, 500),
    ("icon_profile.svg", 1600, 850)
]

# Step 3: Create a figure with the same size as your original JPG
fig = Figure(original_jpg_width, original_jpg_height)

# Step 4: Add each icon to the figure at its specified position
for icon_path, x, y in icon_positions:
    # If your icon isn't already 48x48, add a scale parameter (e.g., scale(0.5) for 96x96 icons)
    fig.append(SVG(icon_path).move(x, y))

# Step 5: Save the final SVG (contains only vector icons, no JPG)
fig.save("final_icons_only.svg")

Key Notes

  • Icon Size Adjustment: If your SVG icons aren’t already 48×48, you can adjust their size using the scale() method. For example: SVG(icon_path).scale(0.5).move(x, y) will shrink a 96×96 icon to 48×48.
  • Relative Positions: If you have percentage-based positions (e.g., "20% from the left"), convert them to absolute coordinates using the JPG dimensions: x = original_jpg_width * 0.2, y = original_jpg_height * 0.3.
  • Alternative Approach: If you prefer not to use svgutils, you can manually manipulate SVG XML with lxml: create a root <svg> element matching the JPG size, then embed each icon’s content (removing their top-level <svg> tags) and add a transform="translate(x y)" attribute to position them. This is more low-level but works just as well.

Final Verdict

This is absolutely feasible, and Python provides straightforward tools to get it done. The end result will be a pure vector SVG file containing only your overlaid icons, with no trace of the original JPG background.

内容的提问来源于stack exchange,提问作者Bil11

火山引擎 最新活动