请写一个python程序,输入是彩色的png图片,输出是经过临摹位图轮廓的svg格式的文件。
Private research annotation
Sign in to add a private research label or note.
- User
请写一个python程序,输入是彩色的png图片,输出是经过临摹位图轮廓的svg格式的文件。
Assistant
PNG 转 SVG 轮廓图程序
以下是一个 Python 程序,使用 Pillow 和 svglib 库将 PNG 图片转换为 SVG 格式的轮廓图。
依赖库
- Pillow (
pip install pillow) - svglib (
pip install svglib) - reportlab (
pip install reportlab)
代码
pythonfrom PIL import Image, ImageOps from svglib.svglib import svglib from reportlab.graphics import renderPM def png_to_svg(input_file, output_file): """ 将 PNG 图片转换为 SVG 格式的轮廓图 """ # 打开 PNG 图片 img = Image.open(input_file) # 转换为灰度图 img_gray = img.convert('L') # 对图像进行阈值化处理 img_threshold = img_gray.point(lambda x: 0 if x < 128 else 255) # 调整图像大小 img_threshold = img_threshold.resize((img_threshold.width // 2, img_threshold.height // 2)) # 保存为 SVG 文件 img_threshold.save('temp.png') drawing = svglib.svg2rlg('temp.png') renderPM.drawToFile(drawing, output_file, 'SVG') # 输入 PNG 图片文件名 input_file = 'input.png' # 输出 SVG 图片文件名 output_file = 'output.svg' png_to_svg(input_file, output_file)- Pillow (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.