python 爬取svg数据_使用Python / PIL读取SVG文件

I'm new to Python so please be patient with me.

Basically my script should run like this:

1) Load an image and split it into R, G, B channels

2) Mark an area of that image with an existing path saved as a svg file and

3) perform some calculations with the R,G,B values of this area.

(further send the the image either to folder A or B, depending on the outcome of the calc)

I've done 1) and parts of 3) but my current problem is to load the svg file and apply it on my image.

The image is a photograph taken with a fisheye lens. I just want to get the RGB-values of the actual photograph - not the black background. I created the svg file by drawing the desired path with GIMP (not a circle).

My question is:

How to load an existing svg-file (and read it as a path) with python and apply the path onto an image?

I'm sure there is a simple way to do that, but I haven't found it yet.

Any hints and reccommendations will be appreciated!

Edit:

The svg file:

/p>

`"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

width="22.2222in" height="16.6667in"

viewBox="0 0 1600 1200">

fill="none" stroke="black" stroke-width="1"

d="M 848.00,103.21

C 848.00,103.21 861.00,103.96 861.00,103.96

891.77,105.30 921.91,109.93 952.00,116.58

988.30,124.59 1023.25,137.24 1057.00,152.69

1210.78,223.11 1322.55,368.09 1353.61,534.00

1358.02,557.54 1361.00,581.11 1362.04,605.00

1362.04,605.00 1363.00,616.00 1363.00,616.00

1363.00,616.00 1363.00,649.00 1363.00,649.00

1363.00,649.00 1362.04,662.00 1362.04,662.00

1360.12,705.99 1352.05,749.92 1338.98,792.00

1289.63,950.77 1167.62,1077.58 1011.00,1133.31

962.90,1150.42 911.98,1160.82 861.00,1163.04

861.00,1163.04 848.00,1164.00 848.00,1164.00

848.00,1164.00 815.00,1164.00 815.00,1164.00

815.00,1164.00 804.00,1163.04 804.00,1163.04

756.47,1160.97 709.15,1151.72 664.00,1136.67

608.47,1118.16 556.55,1090.63 510.00,1055.12

469.68,1024.36 434.97,988.81 404.88,948.00

288.05,789.50 269.64,577.90 355.26,401.00

418.58,270.18 535.32,170.13 674.00,127.02

700.27,118.86 729.73,111.96 757.00,108.28

757.00,108.28 795.00,104.17 795.00,104.17

795.00,104.17 809.00,103.21 809.00,103.21

809.00,103.21 848.00,103.21 848.00,103.21 Z" />

解决方案

You have to rasterize the SVG file to a bitmap and then you can superpose it to the original image using PIL, as it does not support vector images.

I did some research some time ago, and the only library I found which supports this under python is cairo (again, maybe the landscape about SVG rasterizing tools changed in the last months).

The other way around (I don't think this meets your requirements) is to apply your original bitmap image as a background to the SVG one, but this sounds soooo creepy... ;-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python turtle库不支持SVG fill_rule属性,但是可以通过一些方法模拟它的效果。 一种简单的方法是使用Python的绘图库,例如Pillow或OpenCV。这些库可以将SVG文件入并绘制它们。然后,可以使用Python的算法来模拟fill_rule=”evenodd”。 另一种方法是使用Python turtle库中的pencolor函数,并使用“XOR”操作来模拟fill_rule=”evenodd”。当turtle遇到边界时,它将自动填充形状。然而,它没有内部的fill_rule选项来控制填充。 以下是一个使用“XOR”操作来模拟fill_rule=”evenodd”的例子: ``` import turtle def draw_shape(): turtle.penup() turtle.goto(0,0) turtle.pendown() turtle.begin_fill() turtle.fillcolor("red") # Draw shape here turtle.circle(50) turtle.end_fill() # XOR fill the shape turtle.pencolor("white") turtle.fillcolor("white") turtle.begin_fill() turtle.goto(0,0) turtle.pendown() turtle.circle(50) turtle.end_fill() draw_shape() ``` 在这个例子中,我们首先用红色填充一个圆形。然后,我们使用pencolor函数将笔的颜色设置为白色,使用fillcolor函数将填充颜色设置为白色,然后使用begin_fill函数开始一个新的填充。我们移动到圆形的中心,然后画出一个相同大小的圆形。这将使得两个圆形的重叠区域被“XOR”操作,形成一个“evenodd”填充的效果。最后,我们使用end_fill函数结束填充。 这个方法可能不是最完美的,但它是一种可以在Python turtle中模拟fill_rule=”evenodd”的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值