I would think this is a case for calling out to an extension function. There are image libraries that provide such functionality, but I don't know much about them (and this isn't the right place to find out). Calling external functions/methods depends on the XSLT processor you are using.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference