SVG在网页浏览器中渲染正常,但pydiffvg中因缺失stroke属性等问题渲染异常的解决方法问询
SVG在网页浏览器中渲染正常,但pydiffvg中因缺失stroke属性等问题渲染异常的解决方法问询
我最近在用pydiffvg渲染SVG时遇到了个头疼的问题——当SVG里缺少stroke-width这类描边属性时,要么渲染失败,要么颜色不对、元素缺失,但同样的SVG在浏览器或Inkscape里却能完美显示,看起来它们会自动补全这些缺失的属性。
我已经尝试过这些方法,但都没解决问题:
- 给解析器设置默认的描边值(黑色或透明)
- 当
stroke-width未定义时,强制设为1.0
但即使这么做了,pydiffvg还是表现不佳,而浏览器却能轻松处理。我想知道:浏览器处理不完整SVG时到底做了什么不同的操作?我该怎么复现这种行为,让pydiffvg能正确渲染这个SVG?
我使用的SVG代码
<?xml version="1.0" encoding="utf-8"?> <!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools --> <svg width="800px" height="800px" viewBox="0 0 1024 1024" class="icon" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M301.226643 1002.666249A34.773319 34.773319 0 0 1 266.666658 968.106263V399.999833c0-45.226648 71.253304-152.53327 116.266618-209.706579l4.479998-5.759998V109.439954h248.959896v75.093302l4.479999 5.759998C686.079816 247.466564 757.33312 354.773186 757.33312 399.999833v568.10643A34.773319 34.773319 0 0 1 722.559801 1002.666249z" fill="#F05071" /><path d="M615.039846 130.773279V191.99992l9.173329 11.519995c53.759978 68.053305 111.78662 162.559932 111.786621 196.479918v568.10643a13.439994 13.439994 0 0 1-13.226662 13.226661H301.226643a13.439994 13.439994 0 0 1-13.226661-13.226661V399.999833c0-33.706653 58.026642-127.999947 111.78662-196.479918L408.959932 191.99992V130.773279h206.293247m42.666649-42.666649H366.293283V177.066593S245.333333 330.453196 245.333333 399.999833v568.10643A55.89331 55.89331 0 0 0 301.226643 1023.999573h421.333158A55.89331 55.89331 0 0 0 778.666444 968.106263V399.999833c0-69.546638-120.95995-222.93324-120.959949-222.93324V88.10663z" fill="#5C2D51" /><path d="M321.706635 21.333324l380.373175 0 0 106.666623-380.373175 0 0-106.666623Z" fill="#9FDBAD" /><path d="M680.959818 42.666649v63.999973H343.039959V42.666649h337.919859m15.146661-42.666649H327.893299a27.519989 27.519989 0 0 0-27.519989 27.519989v94.293294A27.519989 27.519989 0 0 0 327.893299 149.333271h367.999847a27.519989 27.519989 0 0 0 27.519988-27.519988V27.519989A27.519989 27.519989 0 0 0 695.893146 0z" fill="#5C2D51" /><path d="M266.666658 469.333138h490.666462v341.333191H266.666658z" fill="#9FDBAD" /><path d="M735.999796 490.666462v298.666542H287.999982V490.666462h447.999814m42.666648-42.666649H245.333333v383.99984h533.333111V447.999813z" fill="#5C2D51" /></svg>
渲染效果对比

这是SVG应该呈现的正确效果

这是pydiffvg渲染后的结果(忽略方向,我已经手动调整过了)
我的渲染代码
if shape_type == "custom" and svg_path: try: _, _, parsed_shapes, parsed_shape_groups = svg_to_scene(svg_path) # Filter out invalid shapes parsed_shapes = [ shape for shape in parsed_shapes if hasattr(shape, "points") or hasattr(shape, "center") ] print(f"Parsed {len(parsed_shapes)} valid shapes from {svg_path}.") if not parsed_shapes: print(f"Warning: No valid shapes were parsed from {svg_path}.") continue scale_factor = shape_info.get("size", min(self.canvas_width, self.canvas_height) * 0.01) # Process each logical group based on count for g in range(count): group_shapes = [] for i, (shape, shape_group) in enumerate(zip(parsed_shapes, parsed_shape_groups)): if hasattr(shape, "points"): shape.points = shape.points.to(self.device) * scale_factor shape.initial_points = shape.points.clone() elif hasattr(shape, "center"): shape.center = shape.center.to(self.device) * scale_factor shape.initial_points = shape.center.unsqueeze(0).clone() # Set default colors if missing shape_group.fill_color = ( shape_group.fill_color.clone().to(self.device) if shape_group.fill_color is not None else torch.tensor([1.0, 1.0, 1.0, 1.0], device=self.device) ) shape_group.stroke_color = ( shape_group.stroke_color.clone().to(self.device) if shape_group.stroke_color is not None else shape_group.fill_color.clone().to(self.device) # Default to fill color ) if isinstance(shape, pydiffvg.Path): new_shape = self.copy_path(shape) elif isinstance(shape, pydiffvg.Polygon): new_shape = pydiffvg.Polygon( points=shape.points.clone(), is_closed=shape.is_closed, ) elif isinstance(shape, pydiffvg.Circle): new_shape = pydiffvg.Circle( radius=shape.radius.clone(), center=shape.center.clone(), ) else: print(f"Warning: Unsupported shape type {type(shape)}.") continue new_shape.initial_points = shape.initial_points.clone() group_shapes.append(new_shape) self.shapes.append(new_shape) new_shape_group = pydiffvg.ShapeGroup( shape_ids=torch.tensor([len(self.shapes) - 1], dtype=torch.int32).to(self.device), fill_color=shape_group.fill_color, stroke_color=shape_group.stroke_color, ) self.shape_groups.append(new_shape_group) self.shapes_by_logical_group.append(group_shapes) # Initialize position and orientation position = torch.tensor( [ center_x + (torch.rand(1).item() - 0.5) * 2 * max_radius, center_y + (torch.rand(1).item() - 0.5) * 2 * max_radius, ], requires_grad=True, device=self.device, ) orientation = torch.tensor( [torch.rand(1).item() * 360], requires_grad=True, device=self.device, ) # Apply transformations to group shapes for shape in group_shapes: if hasattr(shape, "initial_points"): angle = torch.deg2rad(orientation[0]) rotation_matrix = torch.tensor([ [torch.cos(angle), -torch.sin(angle)], [torch.sin(angle), torch.cos(angle)] ]).to(self.device) transformed_points = torch.matmul(shape.initial_points, rotation_matrix.T) + position shape.points = transformed_points.clone() elif isinstance(shape, pydiffvg.Circle): shape.center = position.clone() self.parameters.position.append(position) self.parameters.orientation.append(orientation) logical_group_count += 1 except Exception as e: print(f"Error processing custom SVG {svg_path}: {e}") continue
有没有大佬能指点下,该怎么预处理或解析这类SVG,让pydiffvg能正确渲染它?感激不尽!
备注:内容来源于stack exchange,提问作者Ravish Jha
相关产品推荐
相关产品推荐

