Read SVG file with Python/PIL
Asked Answered
T

2

6

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

Truncated answered 30/8, 2010 at 12:10 Comment(3)
If you could share an example image and an svg-file, it would be easier to understand what you intend to do. What kind of calculation are you doing in the 3rd step?Deen
A small update: I haven't found a way to do it with PIL but with GIMPTruncated
@Truncated If you found a way using GIMP then so be it. You can interface with GIMP using Python, so the problem is solved (why would you insist on using a tool that doesn't solve your problem ?).Precentor
E
3

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

Ensoul answered 30/8, 2010 at 17:15 Comment(1)
how would you apply the original image as a bitmap to the svg one?Frederique
D
0

pip install svglib Type this in cmd it willl install svglib A pure-Python library for reading and converting SVG Read book of svglib or doc

Danielledaniels answered 7/1, 2022 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.