Prototyping/Download Sample Cut-up Share: Difference between revisions

From XPUB & Lens-Based wiki
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Challenge: create an Inkscape extension to directly import images from the openclipart site.
(Longer term goal: create an Inkscape extension to directly import images from the openclipart site.)


From the [http://wiki.inkscape.org/wiki/index.php/Script_extensions Inkscape wiki]:
Starting point: Connecting command-line to the creation of a custom Inkscape "effect".
(interpreter)? your_script (--param=value)* /path/to/input[[/SVGfile]] | inkscape


* Command line programs & parsing "arguments"
* Manipulating XML with etree


== Inscape Effects: Hello World ==
[[Creating an Inkscape Effect with Python & ElementTree]]
 
To create/install an inkscape extension, you need to place it in the Inkscape extensions folder ($HOME/.config/inkscape/extensions/ or /usr/share/inkscape/extensions).
 
First thing you need is an "inx" file, an "inkscape-extension" XML file that describes your extension to Inkscape. This is pretty much the simplest form of an effect: NB The "submenu" item selects which Menu your effect will be presented under (in this case, we create a new menu called "Python"), and the "_name" element sets the name of the Effect itself.
 
helloworld.inx
<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Hello World</_name>
<id>pzi.helloworld</id>
<dependency type="executable" location="extensions">helloworld.py</dependency>
    <effect>
        <object-type>all</object-type>
        <effects-menu>
            <submenu _name="Python"/>
        </effects-menu>
    </effect>
    <script>
    <command reldir="extensions" interpreter="python">helloworld.py</command>
    </script>
</inkscape-extension>
</source>
 
Inkscape reads the inx files when it starts, so Inkscape needs to be restarted when an inx file is added or changed. Luckily, changes to the script do not require that Inkscape get restarted.
 
Now, for some code, a "Hello World", that does nothing (yet)...
 
helloworld.py
<source lang="python">
import sys, codecs
from lxml import etree
 
f = codecs.open(sys.argv[-1], encoding="utf-8")
doc = etree.parse(f)
sys.stdout.write(etree.tostring(doc, encoding="utf-8", xml_declaration=True))
</source>
 
helloworld.py (with log)
<source lang="python">
import sys, codecs
from lxml import etree
 
log = open("helloworld.log", "w")
log.write("argv: " + str(sys.argv))
log.close()
 
f = codecs.open(sys.argv[-1], encoding="utf-8")
doc = etree.parse(f)
sys.stdout.write(etree.tostring(doc, encoding="utf-8", xml_declaration=True))
</source>


== ==
== ==
Line 110: Line 59:
</div>""".format(title, link, thumbnail_url)
</div>""".format(title, link, thumbnail_url)
</source>
</source>
== Resources ==
* http://wiki.inkscape.org/wiki/index.php/Script_extensions
* http://docs.python.org/library/argparse.html#module-argparse
* http://lxml.de/tutorial.html
* http://www.w3.org/TR/xpath/
* http://wiki.inkscape.org/wiki/index.php/Script_extensions
* http://wiki.inkscape.org/wiki/index.php/Release_notes/0.47#Extension_API_changes

Latest revision as of 14:19, 26 October 2011

(Longer term goal: create an Inkscape extension to directly import images from the openclipart site.)

Starting point: Connecting command-line to the creation of a custom Inkscape "effect".

  • Command line programs & parsing "arguments"
  • Manipulating XML with etree

Creating an Inkscape Effect with Python & ElementTree

Tools that will be useful: Python, lxml.etree, xpath

http://www.openclipart.org/docs/api

Get some feeds. NB wget's O option (and that's a CAPITAL O), allows to save to a reasonable filename of your choice.

wget http://www.openclipart.org/media/feed/rss/woman -O woman.xml
wget http://www.openclipart.org/media/feed/rss/man -O man.xml


Creating a simple HTML page from the feed

import codecs, sys, lxml.etree, urllib2

# Open Live URL
f = urllib2.urlopen("http://www.openclipart.org/media/feed/rss/woman")

# Open the filename given on the command line
# f = codecs.open(sys.argv[1], encoding="utf-8")

# Read in the XML file
doc = lxml.etree.parse(f)

# This is a Python dictionary containing
# the xml "namespaces" that we may use
NS = {
    'media': 'http://search.yahoo.com/mrss/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'cc': 'http://creativecommons.org/ns#',
    'atom': 'http://www.w3.org/2005/Atom',
}

# Loop over the item elements
for item in doc.xpath("//item"):
    # NB: the "." at the start of the xpaths
    # makes the query relative to the current (context)
    # ie the particular item (and not the whole document)

    svg = item.xpath(".//enclosure/@url")[0]
    thumbnail_url = item.xpath(".//media:thumbnail/@url", namespaces=NS)[0]
    creator = item.xpath(".//dc:creator/text()", namespaces=NS)[0]
    title = item.xpath(".//title/text()")[0]
    link = item.xpath(".//link/text()")[0]

    # Output some HTML
    print """<div>
<a href="{1}"><img src="{2}" />{0}</a>
</div>""".format(title, link, thumbnail_url)