Friday, February 18, 2011

Exporting slices from Inkscape, part 2

I previously wrote about how to export slices from an Inkscape file, using a bash script. Here's a better script, using Python, which doesn't require you to give your slices any special label.

The new process is as follows:

  • First, draw your image as usual.
  • Then, add a layer that will hold the slices; name it slices (this is important). Set the layer's opacity to about 50% to be able to see what you're doing. Enable the grid, and make sure it is set to pixels: you want your slices to align with pixel boundaries.
  • Draw your slice rectangles onto the slices layer, aligned to the grid. Ensure that the rectangles have no border; the fill is irrelevant (I use red).
  • Right-click a slice, and choose Object Properties. Change Id field to the name of the eventual PNG file, without the extension. The area defined by a rectangle named foo will be saved to foo.png. Repeat this for all slices.
  • Hide the slices layer. If you forget this, the script will print a warning.
  • Save your image. Let's say you called it layout.svg.
  • Run the script as follows:
    ./export.py layout.svg
    You should see each of your slices being exported to a PNG file in the same directory.

Here's the code for the script. Save this to a file export.py and make it executable.

#!/usr/bin/env python

import sys
import os
from xml.dom import minidom

if len(sys.argv) < 2:
    print 'Usage:  %s filename.svg' % sys.argv[0]
    sys.exit(0)
input_file = sys.argv[1]

dom = minidom.parse(input_file)
groups = dom.getElementsByTagName('g')
for group in groups:
    if group.getAttribute('inkscape:groupmode') == 'layer' and group.getAttribute('inkscape:label') == 'slices':
        if 'display:none' not in group.getAttribute('style'):
            print 'Warning: slices layer might still be visible'
        for element in group.getElementsByTagName('rect'):
            export_id = element.getAttribute('id')
            filename = '%s.png' % export_id
            print 'Exporting %s...' % filename
            os.system('inkscape --export-id="%s" --export-png="%s" --file="%s"' % (export_id, filename, input_file))
        break
else:
    print 'No layer named "slices" found; not exporting anything'
    sys.exit(1)

Wednesday, February 2, 2011

Editor's Prick?

Some time ago, I received an e-mail concerning my little open source program Taekwindow:

Monday, Jan 17, 2011, 10:27
From: F.R.
To: me

My name is F., Customer Manager of WareSeeker.com, a professional software directory. I would like to propose a solution to promote your software on our site.

We would like to take this opportunity to introduce ourselves. Wareseeker is one of top 5 professional software directories in the world and currently serving about 2 million page views per day. Many renowned software publishers rely on us as their primary media of promoting their products, thus increasing the sales or download dramatically.

We have tested thoroughly and guarantee that Taekwindow 0.3.1 is 100% SAFE TO INSTALL, which means it does not contain any form of malware: spyware, viruses, trojans and dialers.

Taekwindow 0.3.1 has been received Editor’s Pick Award from us.

We hope that you will gain more benefit through this Award. Moreover, we would like to request you add our link and Editor's Pick icon on your Awards category at http://taekwindow.sourceforge.net/download.html

It's not the first time Taekwindow receives such an award, and they generally don't mean very much. But still, it's a nice gesture, and I've done other download sites the same favour, so I replied in the positive.

Since the Taekwindow site is largely built by an automated build system that only runs on Windows, and I am spending all my time in Linux lately, I did not make any promises when I would get round to it. It would probably have been later that week.

Two days later, I received two e-mails in quick succession.

Wed, Jan 19, 2011, 02:40
From: F.R.
To: me

How about our request of our link and Editor's Pick icon at http://taekwindow.sourceforge.net/download.html

You will do that, ok? but when

Keep contact me

Wed, Jan 19, 2011, 09:46
From: F.R.
To: me

How long you got message of mine? I will remind you in final time for our link and Editor's Pick icon on your site.

Give me the respond when you reached this email

Okay, so he expects me to answer to e-mail within 7 hours and 6 minutes, while I'm asleep. And he's getting pushy. But at least I could tell him that I received his mails.

Wed, Jan 19, 2011, 12:04
From: me
To: F.R.

I got your messages alright. I'm a busy person, and I do not appreciate your impatience about me doing you a favour. Please stop pushing me. I'll get to it as soon as I have time.

I might still have done it later that week, but I didn't get round to it. Today, another e-mail appeared.

Tue, Jan 25, 2011, 02:42
From: F.R.
To: me

I don't know how are you busy, I'm also a busy man with a huge business volume each day but I still spend some minutes to write for me to remind you about our request.

Today If you don't reply my mail, I will remove our award for your product on my site. Contact me immediately when you reached this mail

Is that how you treat people when you want them to do you a favour? Not appreciated.

Tue, Jan 25, 2011, 11:45
From: me
To: F.R.

Dear Mr. R.,

The usual nature of awards is that they are given without requesting anything in return. In spite of that, I graciously agreed to return the favour, although due to other obligations I couldn't say when. After a few days, a gentle reminder might certainly have been appropriate. However, two reminders within eight hours (during which I was asleep, I might add), of an increasingly unfriendly nature, did not increase the likelihood that I would answer to your request. Your further pushy behaviour did not improve the situation, and now you request has turned into a demand, with a corresponding punishment should I not comply.

I noticed that Taekwindow has received 13 downloads through your site, and this number has not changed in the last few days. Compared to 149 downloads at Softoxi and 1121 downloads at Softpedia, I doubt there is much value for Taekwindow to be hosted on your site. Given the apparent attitude of the site's employees, it might even do more harm than good.

If it must be like this, I would prefer not to have anything to do with you or your site anymore. I have removed the staged modifications to the Taekwindow website. I will now proceed to write a post about our exchange on my blog, which receives a moderate amount of traffic. Good luck with your future endeavours.

Sincerely,

Thomas

P.S. Incidentally, the download page for Taekwindow on WareSeeker reads: "Software piracy is theft, Using crack, password, serial numbers, registration codes, key generators, cd key, hacks is illegal and prevent future development of Taekwindow 0.3.1 Edition." None of these statements are true. Software piracy may be considered a crime in most countries, but it is distinct from theft. The use of password[s], serial numbers, registration codes and/or cd keys is often a part of software installation required by the software's manufacturer, without which the software will not function, so this can hardly be considered illegal. Finally, the use of any of these will not impact the future development of Taekwindow 0.3.1 in the slightest.

Here's that blog post. See? I do keep my promises — mostly.