Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

path - Read SVG file with Python/PIL

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:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
             `"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

<svg xmlns="http://www.w3.org/2000/svg"
     width="22.2222in" height="16.6667in"
     viewBox="0 0 1600 1200">
  <path id="Auswahl"
        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" />
</svg>

And the link to the image: http://img153.imageshack.us/img153/5330/skybig.jpg

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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... ;-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...